1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2017 Linaro Ltd.
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-contig.h>
18 #include "hfi_venus_io.h"
19 #include "hfi_parser.h"
23 #include "pm_helpers.h"
26 * Three resons to keep MPLANE formats (despite that the number of planes
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
32 static const struct venus_format vdec_formats[] = {
34 .pixfmt = V4L2_PIX_FMT_QC08C,
36 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
38 .pixfmt = V4L2_PIX_FMT_QC10C,
40 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
42 .pixfmt = V4L2_PIX_FMT_NV12,
44 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
46 .pixfmt = V4L2_PIX_FMT_MPEG4,
48 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
49 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
51 .pixfmt = V4L2_PIX_FMT_MPEG2,
53 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
54 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
56 .pixfmt = V4L2_PIX_FMT_H263,
58 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
59 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
61 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
63 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
64 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
66 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
68 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
69 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
71 .pixfmt = V4L2_PIX_FMT_H264,
73 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
74 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
76 .pixfmt = V4L2_PIX_FMT_VP8,
78 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
79 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
81 .pixfmt = V4L2_PIX_FMT_VP9,
83 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
84 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
86 .pixfmt = V4L2_PIX_FMT_XVID,
88 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
89 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
91 .pixfmt = V4L2_PIX_FMT_HEVC,
93 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
94 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
98 static const struct venus_format *
99 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
101 const struct venus_format *fmt = vdec_formats;
102 unsigned int size = ARRAY_SIZE(vdec_formats);
105 for (i = 0; i < size; i++) {
106 if (fmt[i].pixfmt == pixfmt)
110 if (i == size || fmt[i].type != type)
113 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
114 !venus_helper_check_codec(inst, fmt[i].pixfmt))
117 if (V4L2_TYPE_IS_CAPTURE(type) &&
118 !venus_helper_check_format(inst, fmt[i].pixfmt))
121 if (V4L2_TYPE_IS_CAPTURE(type) && fmt[i].pixfmt == V4L2_PIX_FMT_QC10C &&
122 !(inst->bit_depth == VIDC_BITDEPTH_10))
128 static const struct venus_format *
129 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
131 const struct venus_format *fmt = vdec_formats;
132 unsigned int size = ARRAY_SIZE(vdec_formats);
133 unsigned int i, k = 0;
138 for (i = 0; i < size; i++) {
141 if (fmt[i].type != type)
144 if (V4L2_TYPE_IS_OUTPUT(type)) {
145 valid = venus_helper_check_codec(inst, fmt[i].pixfmt);
146 } else if (V4L2_TYPE_IS_CAPTURE(type)) {
147 valid = venus_helper_check_format(inst, fmt[i].pixfmt);
149 if (fmt[i].pixfmt == V4L2_PIX_FMT_QC10C &&
150 !(inst->bit_depth == VIDC_BITDEPTH_10))
154 if (k == index && valid)
166 static const struct venus_format *
167 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
169 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
170 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
171 const struct venus_format *fmt;
174 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
175 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
177 fmt = find_format(inst, pixmp->pixelformat, f->type);
179 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
180 pixmp->pixelformat = V4L2_PIX_FMT_NV12;
181 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
182 pixmp->pixelformat = V4L2_PIX_FMT_H264;
185 fmt = find_format(inst, pixmp->pixelformat, f->type);
188 pixmp->width = clamp(pixmp->width, frame_width_min(inst),
189 frame_width_max(inst));
190 pixmp->height = clamp(pixmp->height, frame_height_min(inst),
191 frame_height_max(inst));
193 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
194 pixmp->height = ALIGN(pixmp->height, 32);
196 if (pixmp->field == V4L2_FIELD_ANY)
197 pixmp->field = V4L2_FIELD_NONE;
198 pixmp->num_planes = fmt->num_planes;
201 szimage = venus_helper_get_framesz(pixmp->pixelformat, pixmp->width,
204 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
205 pfmt[0].sizeimage = szimage;
206 pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
208 pfmt[0].sizeimage = clamp_t(u32, pfmt[0].sizeimage, 0, SZ_8M);
209 pfmt[0].sizeimage = max(pfmt[0].sizeimage, szimage);
210 pfmt[0].bytesperline = 0;
216 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
218 struct venus_inst *inst = to_inst(file);
220 vdec_try_fmt_common(inst, f);
225 static int vdec_check_src_change(struct venus_inst *inst)
229 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE &&
230 inst->codec_state == VENUS_DEC_STATE_INIT &&
234 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE)
238 * The code snippet below is a workaround for backward compatibility
239 * with applications which doesn't support V4L2 events. It will be
240 * dropped in future once those applications are fixed.
243 if (inst->codec_state != VENUS_DEC_STATE_INIT)
246 ret = wait_event_timeout(inst->reconf_wait, inst->reconfig,
247 msecs_to_jiffies(100));
251 if (!(inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) ||
253 dev_dbg(inst->core->dev, VDBGH "wrong state\n");
259 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
261 struct venus_inst *inst = to_inst(file);
262 const struct venus_format *fmt = NULL;
263 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
266 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
268 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
271 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
272 ret = vdec_check_src_change(inst);
277 pixmp->pixelformat = fmt->pixfmt;
279 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
280 pixmp->width = inst->width;
281 pixmp->height = inst->height;
282 pixmp->colorspace = inst->colorspace;
283 pixmp->ycbcr_enc = inst->ycbcr_enc;
284 pixmp->quantization = inst->quantization;
285 pixmp->xfer_func = inst->xfer_func;
286 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
287 pixmp->width = inst->out_width;
288 pixmp->height = inst->out_height;
291 vdec_try_fmt_common(inst, f);
296 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
298 struct venus_inst *inst = to_inst(file);
299 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
300 struct v4l2_pix_format_mplane orig_pixmp;
301 const struct venus_format *fmt;
302 struct v4l2_format format;
303 u32 pixfmt_out = 0, pixfmt_cap = 0;
306 q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type);
315 fmt = vdec_try_fmt_common(inst, f);
317 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
318 pixfmt_out = pixmp->pixelformat;
319 pixfmt_cap = inst->fmt_cap->pixfmt;
320 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
321 pixfmt_cap = pixmp->pixelformat;
322 pixfmt_out = inst->fmt_out->pixfmt;
325 memset(&format, 0, sizeof(format));
327 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
328 format.fmt.pix_mp.pixelformat = pixfmt_out;
329 format.fmt.pix_mp.width = orig_pixmp.width;
330 format.fmt.pix_mp.height = orig_pixmp.height;
331 vdec_try_fmt_common(inst, &format);
333 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
334 inst->out_width = format.fmt.pix_mp.width;
335 inst->out_height = format.fmt.pix_mp.height;
336 inst->colorspace = pixmp->colorspace;
337 inst->ycbcr_enc = pixmp->ycbcr_enc;
338 inst->quantization = pixmp->quantization;
339 inst->xfer_func = pixmp->xfer_func;
340 inst->input_buf_size = pixmp->plane_fmt[0].sizeimage;
343 memset(&format, 0, sizeof(format));
345 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
346 format.fmt.pix_mp.pixelformat = pixfmt_cap;
347 format.fmt.pix_mp.width = orig_pixmp.width;
348 format.fmt.pix_mp.height = orig_pixmp.height;
349 vdec_try_fmt_common(inst, &format);
351 inst->width = format.fmt.pix_mp.width;
352 inst->height = format.fmt.pix_mp.height;
355 inst->crop.width = inst->width;
356 inst->crop.height = inst->height;
358 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
360 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
362 inst->output2_buf_size =
363 venus_helper_get_framesz(pixfmt_cap, orig_pixmp.width, orig_pixmp.height);
370 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
372 struct venus_inst *inst = to_inst(file);
374 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
375 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
382 case V4L2_SEL_TGT_CROP_BOUNDS:
383 case V4L2_SEL_TGT_CROP_DEFAULT:
384 case V4L2_SEL_TGT_CROP:
385 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
387 s->r.width = inst->out_width;
388 s->r.height = inst->out_height;
390 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
391 case V4L2_SEL_TGT_COMPOSE_PADDED:
392 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
394 s->r.width = inst->width;
395 s->r.height = inst->height;
397 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
398 case V4L2_SEL_TGT_COMPOSE:
399 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
411 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
413 strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
414 strscpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
415 strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
420 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
422 struct venus_inst *inst = to_inst(file);
423 const struct venus_format *fmt;
425 memset(f->reserved, 0, sizeof(f->reserved));
427 fmt = find_format_by_index(inst, f->index, f->type);
431 f->pixelformat = fmt->pixfmt;
432 f->flags = fmt->flags;
437 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
439 struct venus_inst *inst = to_inst(file);
440 struct v4l2_captureparm *cap = &a->parm.capture;
441 struct v4l2_fract *timeperframe = &cap->timeperframe;
442 u64 us_per_frame, fps;
444 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
445 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
448 memset(cap->reserved, 0, sizeof(cap->reserved));
449 if (!timeperframe->denominator)
450 timeperframe->denominator = inst->timeperframe.denominator;
451 if (!timeperframe->numerator)
452 timeperframe->numerator = inst->timeperframe.numerator;
453 cap->readbuffers = 0;
454 cap->extendedmode = 0;
455 cap->capability = V4L2_CAP_TIMEPERFRAME;
456 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
457 do_div(us_per_frame, timeperframe->denominator);
462 fps = (u64)USEC_PER_SEC;
463 do_div(fps, us_per_frame);
466 inst->timeperframe = *timeperframe;
471 static int vdec_enum_framesizes(struct file *file, void *fh,
472 struct v4l2_frmsizeenum *fsize)
474 struct venus_inst *inst = to_inst(file);
475 const struct venus_format *fmt;
477 fmt = find_format(inst, fsize->pixel_format,
478 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
480 fmt = find_format(inst, fsize->pixel_format,
481 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
489 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
491 fsize->stepwise.min_width = frame_width_min(inst);
492 fsize->stepwise.max_width = frame_width_max(inst);
493 fsize->stepwise.step_width = frame_width_step(inst);
494 fsize->stepwise.min_height = frame_height_min(inst);
495 fsize->stepwise.max_height = frame_height_max(inst);
496 fsize->stepwise.step_height = frame_height_step(inst);
501 static int vdec_subscribe_event(struct v4l2_fh *fh,
502 const struct v4l2_event_subscription *sub)
504 struct venus_inst *inst = container_of(fh, struct venus_inst, fh);
509 return v4l2_event_subscribe(fh, sub, 2, NULL);
510 case V4L2_EVENT_SOURCE_CHANGE:
511 ret = v4l2_src_change_event_subscribe(fh, sub);
514 inst->subscriptions |= V4L2_EVENT_SOURCE_CHANGE;
516 case V4L2_EVENT_CTRL:
517 return v4l2_ctrl_subscribe_event(fh, sub);
524 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
526 struct venus_inst *inst = to_inst(file);
527 struct hfi_frame_data fdata = {0};
530 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
534 mutex_lock(&inst->lock);
536 if (cmd->cmd == V4L2_DEC_CMD_STOP) {
538 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on
539 * decoder input to signal EOS.
541 if (!(inst->streamon_out && inst->streamon_cap))
544 fdata.buffer_type = HFI_BUFFER_INPUT;
545 fdata.flags |= HFI_BUFFERFLAG_EOS;
546 if (IS_V6(inst->core))
547 fdata.device_addr = 0;
549 fdata.device_addr = 0xdeadb000;
551 ret = hfi_session_process_buf(inst, &fdata);
553 if (!ret && inst->codec_state == VENUS_DEC_STATE_DECODING) {
554 inst->codec_state = VENUS_DEC_STATE_DRAIN;
555 inst->drain_active = true;
560 mutex_unlock(&inst->lock);
564 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
565 .vidioc_querycap = vdec_querycap,
566 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
567 .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
568 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
569 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
570 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
571 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
572 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
573 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
574 .vidioc_g_selection = vdec_g_selection,
575 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
576 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
577 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
578 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
579 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
580 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
581 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
582 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
583 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
584 .vidioc_s_parm = vdec_s_parm,
585 .vidioc_enum_framesizes = vdec_enum_framesizes,
586 .vidioc_subscribe_event = vdec_subscribe_event,
587 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
588 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
589 .vidioc_decoder_cmd = vdec_decoder_cmd,
592 static int vdec_pm_get(struct venus_inst *inst)
594 struct venus_core *core = inst->core;
595 struct device *dev = core->dev_dec;
598 mutex_lock(&core->pm_lock);
599 ret = pm_runtime_resume_and_get(dev);
600 mutex_unlock(&core->pm_lock);
605 static int vdec_pm_put(struct venus_inst *inst, bool autosuspend)
607 struct venus_core *core = inst->core;
608 struct device *dev = core->dev_dec;
611 mutex_lock(&core->pm_lock);
614 ret = pm_runtime_put_autosuspend(dev);
616 ret = pm_runtime_put_sync(dev);
618 mutex_unlock(&core->pm_lock);
620 return ret < 0 ? ret : 0;
623 static int vdec_pm_get_put(struct venus_inst *inst)
625 struct venus_core *core = inst->core;
626 struct device *dev = core->dev_dec;
629 mutex_lock(&core->pm_lock);
631 if (pm_runtime_suspended(dev)) {
632 ret = pm_runtime_resume_and_get(dev);
636 ret = pm_runtime_put_autosuspend(dev);
640 mutex_unlock(&core->pm_lock);
642 return ret < 0 ? ret : 0;
645 static void vdec_pm_touch(struct venus_inst *inst)
647 pm_runtime_mark_last_busy(inst->core->dev_dec);
650 static int vdec_set_properties(struct venus_inst *inst)
652 struct vdec_controls *ctr = &inst->controls.dec;
653 struct hfi_enable en = { .enable = 1 };
654 u32 ptype, decode_order, conceal;
657 if (ctr->post_loop_deb_mode) {
658 ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
659 ret = hfi_session_set_property(inst, ptype, &en);
664 if (ctr->display_delay_enable && ctr->display_delay == 0) {
665 ptype = HFI_PROPERTY_PARAM_VDEC_OUTPUT_ORDER;
666 decode_order = HFI_OUTPUT_ORDER_DECODE;
667 ret = hfi_session_set_property(inst, ptype, &decode_order);
672 ptype = HFI_PROPERTY_PARAM_VDEC_CONCEAL_COLOR;
673 conceal = ctr->conceal_color & 0xffff;
674 conceal |= ((ctr->conceal_color >> 16) & 0xffff) << 10;
675 conceal |= ((ctr->conceal_color >> 32) & 0xffff) << 20;
677 ret = hfi_session_set_property(inst, ptype, &conceal);
684 static int vdec_set_work_route(struct venus_inst *inst)
686 u32 ptype = HFI_PROPERTY_PARAM_WORK_ROUTE;
687 struct hfi_video_work_route wr;
689 if (!IS_V6(inst->core))
692 wr.video_work_route = inst->core->res->num_vpp_pipes;
694 return hfi_session_set_property(inst, ptype, &wr);
697 #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE))
699 static int vdec_output_conf(struct venus_inst *inst)
701 struct venus_core *core = inst->core;
702 struct hfi_enable en = { .enable = 1 };
703 struct hfi_buffer_requirements bufreq;
704 u32 width = inst->width;
705 u32 height = inst->height;
706 u32 out_fmt, out2_fmt;
711 ret = venus_helper_set_work_mode(inst);
715 if (core->res->hfi_version == HFI_VERSION_1XX) {
716 ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
717 ret = hfi_session_set_property(inst, ptype, &en);
722 /* Force searching UBWC formats for bigger then HD resolutions */
723 if (width > 1920 && height > ALIGN(1080, 32))
726 /* For Venus v4/v6 UBWC format is mandatory */
727 if (IS_V4(core) || IS_V6(core))
730 ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt,
735 inst->output_buf_size =
736 venus_helper_get_framesz_raw(out_fmt, width, height);
737 inst->output2_buf_size =
738 venus_helper_get_framesz_raw(out2_fmt, width, height);
740 if (is_ubwc_fmt(out_fmt)) {
741 inst->opb_buftype = HFI_BUFFER_OUTPUT2;
742 inst->opb_fmt = out2_fmt;
743 inst->dpb_buftype = HFI_BUFFER_OUTPUT;
744 inst->dpb_fmt = out_fmt;
745 } else if (is_ubwc_fmt(out2_fmt)) {
746 inst->opb_buftype = HFI_BUFFER_OUTPUT;
747 inst->opb_fmt = out_fmt;
748 inst->dpb_buftype = HFI_BUFFER_OUTPUT2;
749 inst->dpb_fmt = out2_fmt;
751 inst->opb_buftype = HFI_BUFFER_OUTPUT;
752 inst->opb_fmt = out_fmt;
753 inst->dpb_buftype = 0;
757 ret = venus_helper_set_raw_format(inst, inst->opb_fmt,
762 ret = venus_helper_set_format_constraints(inst);
767 ret = venus_helper_set_multistream(inst, false, true);
771 ret = venus_helper_set_raw_format(inst, inst->dpb_fmt,
776 ret = venus_helper_set_output_resolution(inst, width, height,
782 if (IS_V3(core) || IS_V4(core) || IS_V6(core)) {
783 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
787 if (bufreq.size > inst->output_buf_size)
791 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT2,
796 if (bufreq.size > inst->output2_buf_size)
800 if (inst->output2_buf_size) {
801 ret = venus_helper_set_bufsize(inst,
802 inst->output2_buf_size,
808 if (inst->output_buf_size) {
809 ret = venus_helper_set_bufsize(inst,
810 inst->output_buf_size,
817 ret = venus_helper_set_dyn_bufmode(inst);
824 static int vdec_session_init(struct venus_inst *inst)
828 ret = venus_helper_session_init(inst);
829 if (ret == -EALREADY)
834 ret = venus_helper_set_input_resolution(inst, frame_width_min(inst),
835 frame_height_min(inst));
841 hfi_session_deinit(inst);
845 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num,
846 unsigned int *out_num)
848 enum hfi_version ver = inst->core->res->hfi_version;
849 struct hfi_buffer_requirements bufreq;
852 *in_num = *out_num = 0;
854 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
858 *in_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
860 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
864 *out_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
869 static int vdec_queue_setup(struct vb2_queue *q,
870 unsigned int *num_buffers, unsigned int *num_planes,
871 unsigned int sizes[], struct device *alloc_devs[])
873 struct venus_inst *inst = vb2_get_drv_priv(q);
874 struct venus_core *core = inst->core;
875 unsigned int in_num, out_num;
879 unsigned int output_buf_size = venus_helper_get_opb_size(inst);
881 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
882 *num_planes != inst->fmt_out->num_planes)
885 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
886 *num_planes != inst->fmt_cap->num_planes)
889 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
890 sizes[0] < inst->input_buf_size)
893 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
894 sizes[0] < output_buf_size)
900 if (test_bit(0, &core->sys_error)) {
904 ret = wait_event_interruptible(core->sys_err_done,
905 !test_bit(0, &core->sys_error));
910 ret = vdec_pm_get(inst);
914 ret = vdec_session_init(inst);
918 ret = vdec_num_buffers(inst, &in_num, &out_num);
922 ret = vdec_pm_put(inst, false);
927 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
928 *num_planes = inst->fmt_out->num_planes;
929 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
932 sizes[0] = max(sizes[0], inst->input_buf_size);
933 inst->input_buf_size = sizes[0];
934 *num_buffers = max(*num_buffers, in_num);
935 inst->num_input_bufs = *num_buffers;
936 inst->num_output_bufs = out_num;
938 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
939 *num_planes = inst->fmt_cap->num_planes;
940 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
943 inst->output_buf_size = sizes[0];
944 *num_buffers = max(*num_buffers, out_num);
945 inst->num_output_bufs = *num_buffers;
947 mutex_lock(&inst->lock);
948 if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP)
949 inst->codec_state = VENUS_DEC_STATE_STOPPED;
950 mutex_unlock(&inst->lock);
960 vdec_pm_put(inst, false);
964 static int vdec_verify_conf(struct venus_inst *inst)
966 enum hfi_version ver = inst->core->res->hfi_version;
967 struct hfi_buffer_requirements bufreq;
970 if (!inst->num_input_bufs || !inst->num_output_bufs)
973 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
977 if (inst->num_output_bufs < bufreq.count_actual ||
978 inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
981 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
985 if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
991 static int vdec_start_capture(struct venus_inst *inst)
995 if (!inst->streamon_out)
998 if (inst->codec_state == VENUS_DEC_STATE_DECODING) {
1002 venus_helper_queue_dpb_bufs(inst);
1003 venus_helper_process_initial_cap_bufs(inst);
1004 inst->streamon_cap = 1;
1008 if (inst->codec_state != VENUS_DEC_STATE_STOPPED)
1012 ret = vdec_output_conf(inst);
1016 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1017 VB2_MAX_FRAME, VB2_MAX_FRAME);
1021 ret = venus_helper_intbufs_realloc(inst);
1025 venus_pm_load_scale(inst);
1027 inst->next_buf_last = false;
1029 ret = venus_helper_alloc_dpb_bufs(inst);
1033 ret = hfi_session_continue(inst);
1037 ret = venus_helper_queue_dpb_bufs(inst);
1041 ret = venus_helper_process_initial_cap_bufs(inst);
1045 inst->codec_state = VENUS_DEC_STATE_DECODING;
1047 if (inst->drain_active)
1048 inst->codec_state = VENUS_DEC_STATE_DRAIN;
1050 inst->streamon_cap = 1;
1051 inst->sequence_cap = 0;
1052 inst->reconfig = false;
1053 inst->drain_active = false;
1058 venus_helper_free_dpb_bufs(inst);
1063 static int vdec_start_output(struct venus_inst *inst)
1067 if (inst->codec_state == VENUS_DEC_STATE_SEEK) {
1068 ret = venus_helper_process_initial_out_bufs(inst);
1069 if (inst->next_buf_last)
1070 inst->codec_state = VENUS_DEC_STATE_DRC;
1072 inst->codec_state = VENUS_DEC_STATE_DECODING;
1076 if (inst->codec_state == VENUS_DEC_STATE_INIT ||
1077 inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) {
1078 ret = venus_helper_process_initial_out_bufs(inst);
1082 if (inst->codec_state != VENUS_DEC_STATE_DEINIT)
1085 venus_helper_init_instance(inst);
1086 inst->sequence_out = 0;
1087 inst->reconfig = false;
1088 inst->next_buf_last = false;
1090 ret = vdec_set_properties(inst);
1094 ret = vdec_set_work_route(inst);
1098 ret = vdec_output_conf(inst);
1102 ret = vdec_verify_conf(inst);
1106 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1107 VB2_MAX_FRAME, VB2_MAX_FRAME);
1111 ret = venus_helper_vb2_start_streaming(inst);
1115 ret = venus_helper_process_initial_out_bufs(inst);
1119 inst->codec_state = VENUS_DEC_STATE_INIT;
1122 inst->streamon_out = 1;
1126 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1128 struct venus_inst *inst = vb2_get_drv_priv(q);
1131 mutex_lock(&inst->lock);
1133 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1134 ret = vdec_start_capture(inst);
1136 ret = vdec_pm_get(inst);
1140 ret = venus_pm_acquire_core(inst);
1144 ret = vdec_pm_put(inst, true);
1148 ret = vdec_start_output(inst);
1154 mutex_unlock(&inst->lock);
1158 vdec_pm_put(inst, false);
1160 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1161 mutex_unlock(&inst->lock);
1165 static void vdec_cancel_dst_buffers(struct venus_inst *inst)
1167 struct vb2_v4l2_buffer *buf;
1169 while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1170 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1173 static int vdec_stop_capture(struct venus_inst *inst)
1177 switch (inst->codec_state) {
1178 case VENUS_DEC_STATE_DECODING:
1179 ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1181 case VENUS_DEC_STATE_DRAIN:
1182 inst->codec_state = VENUS_DEC_STATE_STOPPED;
1183 inst->drain_active = false;
1185 case VENUS_DEC_STATE_SEEK:
1186 vdec_cancel_dst_buffers(inst);
1188 case VENUS_DEC_STATE_DRC:
1189 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, true);
1190 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1191 venus_helper_free_dpb_bufs(inst);
1200 static int vdec_stop_output(struct venus_inst *inst)
1204 switch (inst->codec_state) {
1205 case VENUS_DEC_STATE_DECODING:
1206 case VENUS_DEC_STATE_DRAIN:
1207 case VENUS_DEC_STATE_STOPPED:
1208 case VENUS_DEC_STATE_DRC:
1209 ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1210 inst->codec_state = VENUS_DEC_STATE_SEEK;
1212 case VENUS_DEC_STATE_INIT:
1213 case VENUS_DEC_STATE_CAPTURE_SETUP:
1214 ret = hfi_session_flush(inst, HFI_FLUSH_INPUT, true);
1223 static void vdec_stop_streaming(struct vb2_queue *q)
1225 struct venus_inst *inst = vb2_get_drv_priv(q);
1228 vdec_pm_get_put(inst);
1230 mutex_lock(&inst->lock);
1232 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1233 ret = vdec_stop_capture(inst);
1235 ret = vdec_stop_output(inst);
1237 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_ERROR);
1239 inst->session_error = 0;
1244 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1245 inst->streamon_out = 0;
1247 inst->streamon_cap = 0;
1250 mutex_unlock(&inst->lock);
1253 static void vdec_session_release(struct venus_inst *inst)
1255 struct venus_core *core = inst->core;
1260 mutex_lock(&inst->lock);
1261 inst->codec_state = VENUS_DEC_STATE_DEINIT;
1263 ret = hfi_session_stop(inst);
1264 abort = (ret && ret != -EINVAL) ? 1 : 0;
1265 ret = hfi_session_unload_res(inst);
1266 abort = (ret && ret != -EINVAL) ? 1 : 0;
1267 ret = venus_helper_unregister_bufs(inst);
1268 abort = (ret && ret != -EINVAL) ? 1 : 0;
1269 ret = venus_helper_intbufs_free(inst);
1270 abort = (ret && ret != -EINVAL) ? 1 : 0;
1271 ret = hfi_session_deinit(inst);
1272 abort = (ret && ret != -EINVAL) ? 1 : 0;
1274 if (inst->session_error || test_bit(0, &core->sys_error))
1278 hfi_session_abort(inst);
1280 venus_helper_free_dpb_bufs(inst);
1281 venus_pm_load_scale(inst);
1282 INIT_LIST_HEAD(&inst->registeredbufs);
1283 mutex_unlock(&inst->lock);
1285 venus_pm_release_core(inst);
1286 vdec_pm_put(inst, false);
1289 static int vdec_buf_init(struct vb2_buffer *vb)
1291 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1295 return venus_helper_vb2_buf_init(vb);
1298 static void vdec_buf_cleanup(struct vb2_buffer *vb)
1300 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1301 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1302 struct venus_buffer *buf = to_venus_buffer(vbuf);
1304 mutex_lock(&inst->lock);
1305 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1306 if (!list_empty(&inst->registeredbufs))
1307 list_del_init(&buf->reg_list);
1308 mutex_unlock(&inst->lock);
1311 if (!inst->buf_count)
1312 vdec_session_release(inst);
1315 static void vdec_vb2_buf_queue(struct vb2_buffer *vb)
1317 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1318 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1319 static const struct v4l2_event eos = { .type = V4L2_EVENT_EOS };
1321 vdec_pm_get_put(inst);
1323 mutex_lock(&inst->lock);
1325 if (inst->next_buf_last && V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1326 inst->codec_state == VENUS_DEC_STATE_DRC) {
1327 vbuf->flags |= V4L2_BUF_FLAG_LAST;
1328 vbuf->sequence = inst->sequence_cap++;
1329 vbuf->field = V4L2_FIELD_NONE;
1330 vb2_set_plane_payload(vb, 0, 0);
1331 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1332 v4l2_event_queue_fh(&inst->fh, &eos);
1333 inst->next_buf_last = false;
1334 mutex_unlock(&inst->lock);
1338 venus_helper_vb2_buf_queue(vb);
1339 mutex_unlock(&inst->lock);
1342 static const struct vb2_ops vdec_vb2_ops = {
1343 .queue_setup = vdec_queue_setup,
1344 .buf_init = vdec_buf_init,
1345 .buf_cleanup = vdec_buf_cleanup,
1346 .buf_prepare = venus_helper_vb2_buf_prepare,
1347 .start_streaming = vdec_start_streaming,
1348 .stop_streaming = vdec_stop_streaming,
1349 .buf_queue = vdec_vb2_buf_queue,
1352 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
1353 u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1354 u32 hfi_flags, u64 timestamp_us)
1356 enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1357 struct vb2_v4l2_buffer *vbuf;
1358 struct vb2_buffer *vb;
1361 vdec_pm_touch(inst);
1363 if (buf_type == HFI_BUFFER_INPUT)
1364 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1366 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1368 vbuf = venus_helper_find_buf(inst, type, tag);
1370 venus_helper_change_dpb_owner(inst, vbuf, type, buf_type, tag);
1374 vbuf->flags = flags;
1375 vbuf->field = V4L2_FIELD_NONE;
1376 vb = &vbuf->vb2_buf;
1378 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1379 vb2_set_plane_payload(vb, 0, bytesused);
1380 vb->planes[0].data_offset = data_offset;
1381 vb->timestamp = timestamp_us * NSEC_PER_USEC;
1382 vbuf->sequence = inst->sequence_cap++;
1384 if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
1385 const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
1387 v4l2_event_queue_fh(&inst->fh, &ev);
1389 if (inst->codec_state == VENUS_DEC_STATE_DRAIN) {
1390 inst->drain_active = false;
1391 inst->codec_state = VENUS_DEC_STATE_STOPPED;
1396 state = VB2_BUF_STATE_ERROR;
1398 vbuf->sequence = inst->sequence_out++;
1401 venus_helper_get_ts_metadata(inst, timestamp_us, vbuf);
1403 if (hfi_flags & HFI_BUFFERFLAG_READONLY)
1404 venus_helper_acquire_buf_ref(vbuf);
1406 if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
1407 state = VB2_BUF_STATE_ERROR;
1409 if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) {
1410 state = VB2_BUF_STATE_ERROR;
1411 vb2_set_plane_payload(vb, 0, 0);
1415 v4l2_m2m_buf_done(vbuf, state);
1418 static void vdec_event_change(struct venus_inst *inst,
1419 struct hfi_event_data *ev_data, bool sufficient)
1421 static const struct v4l2_event ev = {
1422 .type = V4L2_EVENT_SOURCE_CHANGE,
1423 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
1424 struct device *dev = inst->core->dev_dec;
1425 struct v4l2_format format = {};
1427 mutex_lock(&inst->lock);
1429 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1430 format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
1431 format.fmt.pix_mp.width = ev_data->width;
1432 format.fmt.pix_mp.height = ev_data->height;
1434 vdec_try_fmt_common(inst, &format);
1436 inst->width = format.fmt.pix_mp.width;
1437 inst->height = format.fmt.pix_mp.height;
1439 * Some versions of the firmware do not report crop information for
1440 * all codecs. For these cases, set the crop to the coded resolution.
1442 if (ev_data->input_crop.width > 0 && ev_data->input_crop.height > 0) {
1443 inst->crop.left = ev_data->input_crop.left;
1444 inst->crop.top = ev_data->input_crop.top;
1445 inst->crop.width = ev_data->input_crop.width;
1446 inst->crop.height = ev_data->input_crop.height;
1448 inst->crop.left = 0;
1450 inst->crop.width = ev_data->width;
1451 inst->crop.height = ev_data->height;
1454 inst->fw_min_cnt = ev_data->buf_count;
1455 /* overwriting this to 11 for vp9 due to fw bug */
1456 if (inst->hfi_codec == HFI_VIDEO_CODEC_VP9)
1457 inst->fw_min_cnt = 11;
1459 inst->out_width = ev_data->width;
1460 inst->out_height = ev_data->height;
1462 if (inst->bit_depth != ev_data->bit_depth)
1463 inst->bit_depth = ev_data->bit_depth;
1465 if (inst->pic_struct != ev_data->pic_struct)
1466 inst->pic_struct = ev_data->pic_struct;
1468 dev_dbg(dev, VDBGM "event %s sufficient resources (%ux%u)\n",
1469 sufficient ? "" : "not", ev_data->width, ev_data->height);
1471 switch (inst->codec_state) {
1472 case VENUS_DEC_STATE_INIT:
1473 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1475 case VENUS_DEC_STATE_DECODING:
1476 case VENUS_DEC_STATE_DRAIN:
1477 inst->codec_state = VENUS_DEC_STATE_DRC;
1484 * The assumption is that the firmware have to return the last buffer
1485 * before this event is received in the v4l2 driver. Also the firmware
1486 * itself doesn't mark the last decoder output buffer with HFI EOS flag.
1489 if (inst->codec_state == VENUS_DEC_STATE_DRC) {
1492 inst->next_buf_last = true;
1494 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, false);
1496 dev_dbg(dev, VDBGH "flush output error %d\n", ret);
1499 inst->next_buf_last = true;
1500 inst->reconfig = true;
1501 v4l2_event_queue_fh(&inst->fh, &ev);
1502 wake_up(&inst->reconf_wait);
1504 mutex_unlock(&inst->lock);
1507 static void vdec_event_notify(struct venus_inst *inst, u32 event,
1508 struct hfi_event_data *data)
1510 struct venus_core *core = inst->core;
1511 struct device *dev = core->dev_dec;
1513 vdec_pm_touch(inst);
1516 case EVT_SESSION_ERROR:
1517 inst->session_error = true;
1518 venus_helper_vb2_queue_error(inst);
1519 dev_err(dev, "dec: event session error %x\n", inst->error);
1521 case EVT_SYS_EVENT_CHANGE:
1522 switch (data->event_type) {
1523 case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
1524 vdec_event_change(inst, data, true);
1526 case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
1527 vdec_event_change(inst, data, false);
1529 case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
1530 venus_helper_release_buf_ref(inst, data->tag);
1541 static void vdec_flush_done(struct venus_inst *inst)
1543 dev_dbg(inst->core->dev_dec, VDBGH "flush done\n");
1546 static const struct hfi_inst_ops vdec_hfi_ops = {
1547 .buf_done = vdec_buf_done,
1548 .event_notify = vdec_event_notify,
1549 .flush_done = vdec_flush_done,
1552 static void vdec_inst_init(struct venus_inst *inst)
1554 inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1555 inst->fmt_out = &vdec_formats[8];
1556 inst->fmt_cap = &vdec_formats[0];
1557 inst->width = frame_width_min(inst);
1558 inst->height = ALIGN(frame_height_min(inst), 32);
1559 inst->crop.left = 0;
1561 inst->crop.width = inst->width;
1562 inst->crop.height = inst->height;
1563 inst->fw_min_cnt = 8;
1564 inst->out_width = frame_width_min(inst);
1565 inst->out_height = frame_height_min(inst);
1567 inst->timeperframe.numerator = 1;
1568 inst->timeperframe.denominator = 30;
1569 inst->opb_buftype = HFI_BUFFER_OUTPUT;
1572 static void vdec_m2m_device_run(void *priv)
1576 static const struct v4l2_m2m_ops vdec_m2m_ops = {
1577 .device_run = vdec_m2m_device_run,
1578 .job_abort = venus_helper_m2m_job_abort,
1581 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1582 struct vb2_queue *dst_vq)
1584 struct venus_inst *inst = priv;
1587 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1588 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1589 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1590 src_vq->ops = &vdec_vb2_ops;
1591 src_vq->mem_ops = &vb2_dma_contig_memops;
1592 src_vq->drv_priv = inst;
1593 src_vq->buf_struct_size = sizeof(struct venus_buffer);
1594 src_vq->allow_zero_bytesused = 1;
1595 src_vq->min_buffers_needed = 0;
1596 src_vq->dev = inst->core->dev;
1597 ret = vb2_queue_init(src_vq);
1601 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1602 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1603 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1604 dst_vq->ops = &vdec_vb2_ops;
1605 dst_vq->mem_ops = &vb2_dma_contig_memops;
1606 dst_vq->drv_priv = inst;
1607 dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1608 dst_vq->allow_zero_bytesused = 1;
1609 dst_vq->min_buffers_needed = 0;
1610 dst_vq->dev = inst->core->dev;
1611 return vb2_queue_init(dst_vq);
1614 static int vdec_open(struct file *file)
1616 struct venus_core *core = video_drvdata(file);
1617 struct venus_inst *inst;
1620 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1624 INIT_LIST_HEAD(&inst->dpbbufs);
1625 INIT_LIST_HEAD(&inst->registeredbufs);
1626 INIT_LIST_HEAD(&inst->internalbufs);
1627 INIT_LIST_HEAD(&inst->list);
1628 mutex_init(&inst->lock);
1631 inst->session_type = VIDC_SESSION_TYPE_DEC;
1632 inst->num_output_bufs = 1;
1633 inst->codec_state = VENUS_DEC_STATE_DEINIT;
1634 inst->buf_count = 0;
1635 inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1636 inst->core_acquired = false;
1637 inst->bit_depth = VIDC_BITDEPTH_8;
1638 inst->pic_struct = HFI_INTERLACE_FRAME_PROGRESSIVE;
1639 init_waitqueue_head(&inst->reconf_wait);
1640 inst->nonblock = file->f_flags & O_NONBLOCK;
1642 venus_helper_init_instance(inst);
1644 ret = vdec_ctrl_init(inst);
1648 ret = hfi_session_create(inst, &vdec_hfi_ops);
1650 goto err_ctrl_deinit;
1652 vdec_inst_init(inst);
1654 ida_init(&inst->dpb_ids);
1657 * create m2m device for every instance, the m2m context scheduling
1658 * is made by firmware side so we do not need to care about.
1660 inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1661 if (IS_ERR(inst->m2m_dev)) {
1662 ret = PTR_ERR(inst->m2m_dev);
1663 goto err_session_destroy;
1666 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1667 if (IS_ERR(inst->m2m_ctx)) {
1668 ret = PTR_ERR(inst->m2m_ctx);
1669 goto err_m2m_release;
1672 v4l2_fh_init(&inst->fh, core->vdev_dec);
1674 inst->fh.ctrl_handler = &inst->ctrl_handler;
1675 v4l2_fh_add(&inst->fh);
1676 inst->fh.m2m_ctx = inst->m2m_ctx;
1677 file->private_data = &inst->fh;
1682 v4l2_m2m_release(inst->m2m_dev);
1683 err_session_destroy:
1684 hfi_session_destroy(inst);
1686 vdec_ctrl_deinit(inst);
1692 static int vdec_close(struct file *file)
1694 struct venus_inst *inst = to_inst(file);
1698 v4l2_m2m_ctx_release(inst->m2m_ctx);
1699 v4l2_m2m_release(inst->m2m_dev);
1700 vdec_ctrl_deinit(inst);
1701 ida_destroy(&inst->dpb_ids);
1702 hfi_session_destroy(inst);
1703 mutex_destroy(&inst->lock);
1704 v4l2_fh_del(&inst->fh);
1705 v4l2_fh_exit(&inst->fh);
1707 vdec_pm_put(inst, false);
1713 static const struct v4l2_file_operations vdec_fops = {
1714 .owner = THIS_MODULE,
1716 .release = vdec_close,
1717 .unlocked_ioctl = video_ioctl2,
1718 .poll = v4l2_m2m_fop_poll,
1719 .mmap = v4l2_m2m_fop_mmap,
1722 static int vdec_probe(struct platform_device *pdev)
1724 struct device *dev = &pdev->dev;
1725 struct video_device *vdev;
1726 struct venus_core *core;
1730 return -EPROBE_DEFER;
1732 core = dev_get_drvdata(dev->parent);
1734 return -EPROBE_DEFER;
1736 platform_set_drvdata(pdev, core);
1738 if (core->pm_ops->vdec_get) {
1739 ret = core->pm_ops->vdec_get(dev);
1744 vdev = video_device_alloc();
1748 strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1749 vdev->release = video_device_release;
1750 vdev->fops = &vdec_fops;
1751 vdev->ioctl_ops = &vdec_ioctl_ops;
1752 vdev->vfl_dir = VFL_DIR_M2M;
1753 vdev->v4l2_dev = &core->v4l2_dev;
1754 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1756 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1758 goto err_vdev_release;
1760 core->vdev_dec = vdev;
1761 core->dev_dec = dev;
1763 video_set_drvdata(vdev, core);
1764 pm_runtime_set_autosuspend_delay(dev, 2000);
1765 pm_runtime_use_autosuspend(dev);
1766 pm_runtime_enable(dev);
1771 video_device_release(vdev);
1775 static int vdec_remove(struct platform_device *pdev)
1777 struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1779 video_unregister_device(core->vdev_dec);
1780 pm_runtime_disable(core->dev_dec);
1782 if (core->pm_ops->vdec_put)
1783 core->pm_ops->vdec_put(core->dev_dec);
1788 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1790 struct venus_core *core = dev_get_drvdata(dev);
1791 const struct venus_pm_ops *pm_ops = core->pm_ops;
1794 if (pm_ops->vdec_power)
1795 ret = pm_ops->vdec_power(dev, POWER_OFF);
1800 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1802 struct venus_core *core = dev_get_drvdata(dev);
1803 const struct venus_pm_ops *pm_ops = core->pm_ops;
1806 if (pm_ops->vdec_power)
1807 ret = pm_ops->vdec_power(dev, POWER_ON);
1812 static const struct dev_pm_ops vdec_pm_ops = {
1813 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1814 pm_runtime_force_resume)
1815 SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1818 static const struct of_device_id vdec_dt_match[] = {
1819 { .compatible = "venus-decoder" },
1822 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1824 static struct platform_driver qcom_venus_dec_driver = {
1825 .probe = vdec_probe,
1826 .remove = vdec_remove,
1828 .name = "qcom-venus-decoder",
1829 .of_match_table = vdec_dt_match,
1833 module_platform_driver(qcom_venus_dec_driver);
1835 MODULE_ALIAS("platform:qcom-venus-decoder");
1836 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1837 MODULE_LICENSE("GPL v2");