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-sg.h>
18 #include "hfi_venus_io.h"
19 #include "hfi_parser.h"
25 * Three resons to keep MPLANE formats (despite that the number of planes
27 * - the MPLANE formats allow only one plane to be used
28 * - the downstream driver use MPLANE formats too
29 * - future firmware versions could add support for >1 planes
31 static const struct venus_format vdec_formats[] = {
33 .pixfmt = V4L2_PIX_FMT_NV12,
35 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
37 .pixfmt = V4L2_PIX_FMT_MPEG4,
39 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
41 .pixfmt = V4L2_PIX_FMT_MPEG2,
43 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
45 .pixfmt = V4L2_PIX_FMT_H263,
47 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
49 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
51 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
53 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
55 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
57 .pixfmt = V4L2_PIX_FMT_H264,
59 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
61 .pixfmt = V4L2_PIX_FMT_VP8,
63 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
65 .pixfmt = V4L2_PIX_FMT_VP9,
67 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
69 .pixfmt = V4L2_PIX_FMT_XVID,
71 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
73 .pixfmt = V4L2_PIX_FMT_HEVC,
75 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
79 static const struct venus_format *
80 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
82 const struct venus_format *fmt = vdec_formats;
83 unsigned int size = ARRAY_SIZE(vdec_formats);
86 for (i = 0; i < size; i++) {
87 if (fmt[i].pixfmt == pixfmt)
91 if (i == size || fmt[i].type != type)
94 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
95 !venus_helper_check_codec(inst, fmt[i].pixfmt))
101 static const struct venus_format *
102 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
104 const struct venus_format *fmt = vdec_formats;
105 unsigned int size = ARRAY_SIZE(vdec_formats);
106 unsigned int i, k = 0;
111 for (i = 0; i < size; i++) {
114 if (fmt[i].type != type)
116 valid = type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
117 venus_helper_check_codec(inst, fmt[i].pixfmt);
118 if (k == index && valid)
130 static const struct venus_format *
131 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
133 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
134 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
135 const struct venus_format *fmt;
138 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
139 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
141 fmt = find_format(inst, pixmp->pixelformat, f->type);
143 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
144 pixmp->pixelformat = V4L2_PIX_FMT_NV12;
145 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
146 pixmp->pixelformat = V4L2_PIX_FMT_H264;
149 fmt = find_format(inst, pixmp->pixelformat, f->type);
152 pixmp->width = clamp(pixmp->width, frame_width_min(inst),
153 frame_width_max(inst));
154 pixmp->height = clamp(pixmp->height, frame_height_min(inst),
155 frame_height_max(inst));
157 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
158 pixmp->height = ALIGN(pixmp->height, 32);
160 if (pixmp->field == V4L2_FIELD_ANY)
161 pixmp->field = V4L2_FIELD_NONE;
162 pixmp->num_planes = fmt->num_planes;
165 szimage = venus_helper_get_framesz(pixmp->pixelformat, pixmp->width,
168 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
169 pfmt[0].sizeimage = szimage;
170 pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
172 pfmt[0].sizeimage = clamp_t(u32, pfmt[0].sizeimage, 0, SZ_8M);
173 pfmt[0].sizeimage = max(pfmt[0].sizeimage, szimage);
174 pfmt[0].bytesperline = 0;
180 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
182 struct venus_inst *inst = to_inst(file);
184 vdec_try_fmt_common(inst, f);
189 static int vdec_check_src_change(struct venus_inst *inst)
193 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE &&
194 inst->codec_state == VENUS_DEC_STATE_INIT &&
198 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE)
202 * The code snippet below is a workaround for backward compatibility
203 * with applications which doesn't support V4L2 events. It will be
204 * dropped in future once those applications are fixed.
207 if (inst->codec_state != VENUS_DEC_STATE_INIT)
210 ret = wait_event_timeout(inst->reconf_wait, inst->reconfig,
211 msecs_to_jiffies(100));
215 if (!(inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) ||
217 dev_dbg(inst->core->dev, "%s: wrong state\n", __func__);
223 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
225 struct venus_inst *inst = to_inst(file);
226 const struct venus_format *fmt = NULL;
227 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
230 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
232 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
235 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
236 ret = vdec_check_src_change(inst);
241 pixmp->pixelformat = fmt->pixfmt;
243 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
244 pixmp->width = inst->width;
245 pixmp->height = inst->height;
246 pixmp->colorspace = inst->colorspace;
247 pixmp->ycbcr_enc = inst->ycbcr_enc;
248 pixmp->quantization = inst->quantization;
249 pixmp->xfer_func = inst->xfer_func;
250 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
251 pixmp->width = inst->out_width;
252 pixmp->height = inst->out_height;
255 vdec_try_fmt_common(inst, f);
260 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
262 struct venus_inst *inst = to_inst(file);
263 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
264 struct v4l2_pix_format_mplane orig_pixmp;
265 const struct venus_format *fmt;
266 struct v4l2_format format;
267 u32 pixfmt_out = 0, pixfmt_cap = 0;
271 fmt = vdec_try_fmt_common(inst, f);
273 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
274 pixfmt_out = pixmp->pixelformat;
275 pixfmt_cap = inst->fmt_cap->pixfmt;
276 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
277 pixfmt_cap = pixmp->pixelformat;
278 pixfmt_out = inst->fmt_out->pixfmt;
281 memset(&format, 0, sizeof(format));
283 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
284 format.fmt.pix_mp.pixelformat = pixfmt_out;
285 format.fmt.pix_mp.width = orig_pixmp.width;
286 format.fmt.pix_mp.height = orig_pixmp.height;
287 vdec_try_fmt_common(inst, &format);
289 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
290 inst->out_width = format.fmt.pix_mp.width;
291 inst->out_height = format.fmt.pix_mp.height;
292 inst->colorspace = pixmp->colorspace;
293 inst->ycbcr_enc = pixmp->ycbcr_enc;
294 inst->quantization = pixmp->quantization;
295 inst->xfer_func = pixmp->xfer_func;
296 inst->input_buf_size = pixmp->plane_fmt[0].sizeimage;
299 memset(&format, 0, sizeof(format));
301 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
302 format.fmt.pix_mp.pixelformat = pixfmt_cap;
303 format.fmt.pix_mp.width = orig_pixmp.width;
304 format.fmt.pix_mp.height = orig_pixmp.height;
305 vdec_try_fmt_common(inst, &format);
307 inst->width = format.fmt.pix_mp.width;
308 inst->height = format.fmt.pix_mp.height;
310 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
312 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
319 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
321 struct venus_inst *inst = to_inst(file);
323 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
324 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
328 case V4L2_SEL_TGT_CROP_BOUNDS:
329 case V4L2_SEL_TGT_CROP_DEFAULT:
330 case V4L2_SEL_TGT_CROP:
331 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
333 s->r.width = inst->out_width;
334 s->r.height = inst->out_height;
336 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
337 case V4L2_SEL_TGT_COMPOSE_PADDED:
338 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
340 s->r.width = inst->width;
341 s->r.height = inst->height;
343 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
344 case V4L2_SEL_TGT_COMPOSE:
345 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
347 s->r.width = inst->out_width;
348 s->r.height = inst->out_height;
361 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
363 strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
364 strscpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
365 strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
370 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
372 struct venus_inst *inst = to_inst(file);
373 const struct venus_format *fmt;
375 memset(f->reserved, 0, sizeof(f->reserved));
377 fmt = find_format_by_index(inst, f->index, f->type);
381 f->pixelformat = fmt->pixfmt;
386 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
388 struct venus_inst *inst = to_inst(file);
389 struct v4l2_captureparm *cap = &a->parm.capture;
390 struct v4l2_fract *timeperframe = &cap->timeperframe;
391 u64 us_per_frame, fps;
393 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
394 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
397 memset(cap->reserved, 0, sizeof(cap->reserved));
398 if (!timeperframe->denominator)
399 timeperframe->denominator = inst->timeperframe.denominator;
400 if (!timeperframe->numerator)
401 timeperframe->numerator = inst->timeperframe.numerator;
402 cap->readbuffers = 0;
403 cap->extendedmode = 0;
404 cap->capability = V4L2_CAP_TIMEPERFRAME;
405 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
406 do_div(us_per_frame, timeperframe->denominator);
411 fps = (u64)USEC_PER_SEC;
412 do_div(fps, us_per_frame);
415 inst->timeperframe = *timeperframe;
420 static int vdec_enum_framesizes(struct file *file, void *fh,
421 struct v4l2_frmsizeenum *fsize)
423 struct venus_inst *inst = to_inst(file);
424 const struct venus_format *fmt;
426 fmt = find_format(inst, fsize->pixel_format,
427 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
429 fmt = find_format(inst, fsize->pixel_format,
430 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
438 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
440 fsize->stepwise.min_width = frame_width_min(inst);
441 fsize->stepwise.max_width = frame_width_max(inst);
442 fsize->stepwise.step_width = frame_width_step(inst);
443 fsize->stepwise.min_height = frame_height_min(inst);
444 fsize->stepwise.max_height = frame_height_max(inst);
445 fsize->stepwise.step_height = frame_height_step(inst);
450 static int vdec_subscribe_event(struct v4l2_fh *fh,
451 const struct v4l2_event_subscription *sub)
453 struct venus_inst *inst = container_of(fh, struct venus_inst, fh);
458 return v4l2_event_subscribe(fh, sub, 2, NULL);
459 case V4L2_EVENT_SOURCE_CHANGE:
460 ret = v4l2_src_change_event_subscribe(fh, sub);
463 inst->subscriptions |= V4L2_EVENT_SOURCE_CHANGE;
465 case V4L2_EVENT_CTRL:
466 return v4l2_ctrl_subscribe_event(fh, sub);
473 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
475 struct venus_inst *inst = to_inst(file);
476 struct hfi_frame_data fdata = {0};
479 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
483 mutex_lock(&inst->lock);
485 if (cmd->cmd == V4L2_DEC_CMD_STOP) {
487 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on
488 * decoder input to signal EOS.
490 if (!(inst->streamon_out && inst->streamon_cap))
493 fdata.buffer_type = HFI_BUFFER_INPUT;
494 fdata.flags |= HFI_BUFFERFLAG_EOS;
495 fdata.device_addr = 0xdeadb000;
497 ret = hfi_session_process_buf(inst, &fdata);
499 if (!ret && inst->codec_state == VENUS_DEC_STATE_DECODING)
500 inst->codec_state = VENUS_DEC_STATE_DRAIN;
504 mutex_unlock(&inst->lock);
508 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
509 .vidioc_querycap = vdec_querycap,
510 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
511 .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
512 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
513 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
514 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
515 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
516 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
517 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
518 .vidioc_g_selection = vdec_g_selection,
519 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
520 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
521 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
522 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
523 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
524 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
525 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
526 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
527 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
528 .vidioc_s_parm = vdec_s_parm,
529 .vidioc_enum_framesizes = vdec_enum_framesizes,
530 .vidioc_subscribe_event = vdec_subscribe_event,
531 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
532 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
533 .vidioc_decoder_cmd = vdec_decoder_cmd,
536 static int vdec_set_properties(struct venus_inst *inst)
538 struct vdec_controls *ctr = &inst->controls.dec;
539 struct hfi_enable en = { .enable = 1 };
543 if (ctr->post_loop_deb_mode) {
544 ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
545 ret = hfi_session_set_property(inst, ptype, &en);
553 #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE))
555 static int vdec_output_conf(struct venus_inst *inst)
557 struct venus_core *core = inst->core;
558 struct hfi_enable en = { .enable = 1 };
559 u32 width = inst->out_width;
560 u32 height = inst->out_height;
561 u32 out_fmt, out2_fmt;
566 ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
570 ret = venus_helper_set_core_usage(inst, VIDC_CORE_ID_1);
574 if (core->res->hfi_version == HFI_VERSION_1XX) {
575 ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
576 ret = hfi_session_set_property(inst, ptype, &en);
581 /* Force searching UBWC formats for bigger then HD resolutions */
582 if (width > 1920 && height > ALIGN(1080, 32))
585 /* For Venus v4 UBWC format is mandatory */
589 ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt,
594 inst->output_buf_size =
595 venus_helper_get_framesz_raw(out_fmt, width, height);
596 inst->output2_buf_size =
597 venus_helper_get_framesz_raw(out2_fmt, width, height);
599 if (is_ubwc_fmt(out_fmt)) {
600 inst->opb_buftype = HFI_BUFFER_OUTPUT2;
601 inst->opb_fmt = out2_fmt;
602 inst->dpb_buftype = HFI_BUFFER_OUTPUT;
603 inst->dpb_fmt = out_fmt;
604 } else if (is_ubwc_fmt(out2_fmt)) {
605 inst->opb_buftype = HFI_BUFFER_OUTPUT;
606 inst->opb_fmt = out_fmt;
607 inst->dpb_buftype = HFI_BUFFER_OUTPUT2;
608 inst->dpb_fmt = out2_fmt;
610 inst->opb_buftype = HFI_BUFFER_OUTPUT;
611 inst->opb_fmt = out_fmt;
612 inst->dpb_buftype = 0;
616 ret = venus_helper_set_raw_format(inst, inst->opb_fmt,
622 ret = venus_helper_set_multistream(inst, false, true);
626 ret = venus_helper_set_raw_format(inst, inst->dpb_fmt,
631 ret = venus_helper_set_output_resolution(inst, width, height,
637 if (IS_V3(core) || IS_V4(core)) {
638 if (inst->output2_buf_size) {
639 ret = venus_helper_set_bufsize(inst,
640 inst->output2_buf_size,
646 if (inst->output_buf_size) {
647 ret = venus_helper_set_bufsize(inst,
648 inst->output_buf_size,
655 ret = venus_helper_set_dyn_bufmode(inst);
662 static int vdec_session_init(struct venus_inst *inst)
666 ret = hfi_session_init(inst, inst->fmt_out->pixfmt);
672 ret = venus_helper_set_input_resolution(inst, frame_width_min(inst),
673 frame_height_min(inst));
679 hfi_session_deinit(inst);
683 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num,
684 unsigned int *out_num)
686 enum hfi_version ver = inst->core->res->hfi_version;
687 struct hfi_buffer_requirements bufreq;
690 *in_num = *out_num = 0;
692 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
696 *in_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
698 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
702 *out_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
707 static int vdec_queue_setup(struct vb2_queue *q,
708 unsigned int *num_buffers, unsigned int *num_planes,
709 unsigned int sizes[], struct device *alloc_devs[])
711 struct venus_inst *inst = vb2_get_drv_priv(q);
712 unsigned int in_num, out_num;
716 unsigned int output_buf_size = venus_helper_get_opb_size(inst);
718 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
719 *num_planes != inst->fmt_out->num_planes)
722 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
723 *num_planes != inst->fmt_cap->num_planes)
726 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
727 sizes[0] < inst->input_buf_size)
730 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
731 sizes[0] < output_buf_size)
737 ret = vdec_session_init(inst);
741 ret = vdec_num_buffers(inst, &in_num, &out_num);
746 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
747 *num_planes = inst->fmt_out->num_planes;
748 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
751 sizes[0] = max(sizes[0], inst->input_buf_size);
752 inst->input_buf_size = sizes[0];
753 *num_buffers = max(*num_buffers, in_num);
754 inst->num_input_bufs = *num_buffers;
755 inst->num_output_bufs = out_num;
757 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
758 *num_planes = inst->fmt_cap->num_planes;
759 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
762 inst->output_buf_size = sizes[0];
763 *num_buffers = max(*num_buffers, out_num);
764 inst->num_output_bufs = *num_buffers;
766 mutex_lock(&inst->lock);
767 if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP)
768 inst->codec_state = VENUS_DEC_STATE_STOPPED;
769 mutex_unlock(&inst->lock);
779 static int vdec_verify_conf(struct venus_inst *inst)
781 enum hfi_version ver = inst->core->res->hfi_version;
782 struct hfi_buffer_requirements bufreq;
785 if (!inst->num_input_bufs || !inst->num_output_bufs)
788 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
792 if (inst->num_output_bufs < bufreq.count_actual ||
793 inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
796 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
800 if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
806 static int vdec_start_capture(struct venus_inst *inst)
810 if (!inst->streamon_out)
813 if (inst->codec_state == VENUS_DEC_STATE_DECODING) {
817 venus_helper_queue_dpb_bufs(inst);
818 venus_helper_process_initial_cap_bufs(inst);
819 inst->streamon_cap = 1;
823 if (inst->codec_state != VENUS_DEC_STATE_STOPPED)
827 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT);
831 ret = vdec_output_conf(inst);
835 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
836 VB2_MAX_FRAME, VB2_MAX_FRAME);
840 ret = venus_helper_intbufs_realloc(inst);
844 ret = venus_helper_alloc_dpb_bufs(inst);
848 ret = venus_helper_queue_dpb_bufs(inst);
852 ret = venus_helper_process_initial_cap_bufs(inst);
856 venus_helper_load_scale_clocks(inst->core);
858 ret = hfi_session_continue(inst);
862 inst->codec_state = VENUS_DEC_STATE_DECODING;
864 inst->streamon_cap = 1;
865 inst->sequence_cap = 0;
866 inst->reconfig = false;
871 venus_helper_free_dpb_bufs(inst);
876 static int vdec_start_output(struct venus_inst *inst)
880 if (inst->codec_state == VENUS_DEC_STATE_SEEK) {
881 ret = venus_helper_process_initial_out_bufs(inst);
882 inst->codec_state = VENUS_DEC_STATE_DECODING;
886 if (inst->codec_state == VENUS_DEC_STATE_INIT ||
887 inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) {
888 ret = venus_helper_process_initial_out_bufs(inst);
892 if (inst->codec_state != VENUS_DEC_STATE_DEINIT)
895 venus_helper_init_instance(inst);
896 inst->sequence_out = 0;
897 inst->reconfig = false;
899 ret = vdec_set_properties(inst);
903 ret = vdec_output_conf(inst);
907 ret = vdec_verify_conf(inst);
911 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
912 VB2_MAX_FRAME, VB2_MAX_FRAME);
916 ret = venus_helper_vb2_start_streaming(inst);
920 ret = venus_helper_process_initial_out_bufs(inst);
924 inst->codec_state = VENUS_DEC_STATE_INIT;
927 inst->streamon_out = 1;
931 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
933 struct venus_inst *inst = vb2_get_drv_priv(q);
936 mutex_lock(&inst->lock);
938 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
939 ret = vdec_start_capture(inst);
941 ret = vdec_start_output(inst);
946 mutex_unlock(&inst->lock);
950 venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
951 mutex_unlock(&inst->lock);
955 static void vdec_cancel_dst_buffers(struct venus_inst *inst)
957 struct vb2_v4l2_buffer *buf;
959 while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
960 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
963 static int vdec_stop_capture(struct venus_inst *inst)
967 switch (inst->codec_state) {
968 case VENUS_DEC_STATE_DECODING:
969 ret = hfi_session_flush(inst, HFI_FLUSH_ALL);
971 case VENUS_DEC_STATE_DRAIN:
972 vdec_cancel_dst_buffers(inst);
973 inst->codec_state = VENUS_DEC_STATE_STOPPED;
975 case VENUS_DEC_STATE_DRC:
976 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT);
977 vdec_cancel_dst_buffers(inst);
978 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
979 INIT_LIST_HEAD(&inst->registeredbufs);
980 venus_helper_free_dpb_bufs(inst);
989 static int vdec_stop_output(struct venus_inst *inst)
993 switch (inst->codec_state) {
994 case VENUS_DEC_STATE_DECODING:
995 case VENUS_DEC_STATE_DRAIN:
996 case VENUS_DEC_STATE_STOPPED:
997 ret = hfi_session_flush(inst, HFI_FLUSH_ALL);
998 inst->codec_state = VENUS_DEC_STATE_SEEK;
1000 case VENUS_DEC_STATE_INIT:
1001 case VENUS_DEC_STATE_CAPTURE_SETUP:
1002 ret = hfi_session_flush(inst, HFI_FLUSH_INPUT);
1011 static void vdec_stop_streaming(struct vb2_queue *q)
1013 struct venus_inst *inst = vb2_get_drv_priv(q);
1016 mutex_lock(&inst->lock);
1018 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1019 ret = vdec_stop_capture(inst);
1021 ret = vdec_stop_output(inst);
1023 venus_helper_buffers_done(inst, VB2_BUF_STATE_ERROR);
1028 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1029 inst->streamon_out = 0;
1031 inst->streamon_cap = 0;
1034 mutex_unlock(&inst->lock);
1037 static void vdec_session_release(struct venus_inst *inst)
1039 struct venus_core *core = inst->core;
1042 mutex_lock(&inst->lock);
1044 inst->codec_state = VENUS_DEC_STATE_DEINIT;
1046 ret = hfi_session_stop(inst);
1047 abort = (ret && ret != -EINVAL) ? 1 : 0;
1048 ret = hfi_session_unload_res(inst);
1049 abort = (ret && ret != -EINVAL) ? 1 : 0;
1050 ret = venus_helper_unregister_bufs(inst);
1051 abort = (ret && ret != -EINVAL) ? 1 : 0;
1052 ret = venus_helper_intbufs_free(inst);
1053 abort = (ret && ret != -EINVAL) ? 1 : 0;
1054 ret = hfi_session_deinit(inst);
1055 abort = (ret && ret != -EINVAL) ? 1 : 0;
1057 if (inst->session_error || core->sys_error)
1061 hfi_session_abort(inst);
1063 venus_helper_free_dpb_bufs(inst);
1064 venus_helper_load_scale_clocks(core);
1065 INIT_LIST_HEAD(&inst->registeredbufs);
1067 mutex_unlock(&inst->lock);
1070 static int vdec_buf_init(struct vb2_buffer *vb)
1072 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1076 return venus_helper_vb2_buf_init(vb);
1079 static void vdec_buf_cleanup(struct vb2_buffer *vb)
1081 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1084 if (!inst->buf_count)
1085 vdec_session_release(inst);
1088 static const struct vb2_ops vdec_vb2_ops = {
1089 .queue_setup = vdec_queue_setup,
1090 .buf_init = vdec_buf_init,
1091 .buf_cleanup = vdec_buf_cleanup,
1092 .buf_prepare = venus_helper_vb2_buf_prepare,
1093 .start_streaming = vdec_start_streaming,
1094 .stop_streaming = vdec_stop_streaming,
1095 .buf_queue = venus_helper_vb2_buf_queue,
1098 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
1099 u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1100 u32 hfi_flags, u64 timestamp_us)
1102 enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1103 struct vb2_v4l2_buffer *vbuf;
1104 struct vb2_buffer *vb;
1107 if (buf_type == HFI_BUFFER_INPUT)
1108 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1110 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1112 vbuf = venus_helper_find_buf(inst, type, tag);
1116 vbuf->flags = flags;
1117 vbuf->field = V4L2_FIELD_NONE;
1118 vb = &vbuf->vb2_buf;
1120 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1121 vb2_set_plane_payload(vb, 0, bytesused);
1122 vb->planes[0].data_offset = data_offset;
1123 vb->timestamp = timestamp_us * NSEC_PER_USEC;
1124 vbuf->sequence = inst->sequence_cap++;
1126 if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
1127 const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
1129 v4l2_event_queue_fh(&inst->fh, &ev);
1131 if (inst->codec_state == VENUS_DEC_STATE_DRAIN)
1132 inst->codec_state = VENUS_DEC_STATE_STOPPED;
1135 vbuf->sequence = inst->sequence_out++;
1138 venus_helper_get_ts_metadata(inst, timestamp_us, vbuf);
1140 if (hfi_flags & HFI_BUFFERFLAG_READONLY)
1141 venus_helper_acquire_buf_ref(vbuf);
1143 if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
1144 state = VB2_BUF_STATE_ERROR;
1146 if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) {
1147 state = VB2_BUF_STATE_ERROR;
1148 vb2_set_plane_payload(vb, 0, 0);
1152 v4l2_m2m_buf_done(vbuf, state);
1155 static void vdec_event_change(struct venus_inst *inst,
1156 struct hfi_event_data *ev_data, bool sufficient)
1158 static const struct v4l2_event ev = {
1159 .type = V4L2_EVENT_SOURCE_CHANGE,
1160 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
1161 struct device *dev = inst->core->dev_dec;
1162 struct v4l2_format format = {};
1164 mutex_lock(&inst->lock);
1166 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1167 format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
1168 format.fmt.pix_mp.width = ev_data->width;
1169 format.fmt.pix_mp.height = ev_data->height;
1171 vdec_try_fmt_common(inst, &format);
1173 inst->width = format.fmt.pix_mp.width;
1174 inst->height = format.fmt.pix_mp.height;
1176 inst->out_width = ev_data->width;
1177 inst->out_height = ev_data->height;
1179 dev_dbg(dev, "event %s sufficient resources (%ux%u)\n",
1180 sufficient ? "" : "not", ev_data->width, ev_data->height);
1183 hfi_session_continue(inst);
1185 switch (inst->codec_state) {
1186 case VENUS_DEC_STATE_INIT:
1187 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1189 case VENUS_DEC_STATE_DECODING:
1190 inst->codec_state = VENUS_DEC_STATE_DRC;
1197 inst->reconfig = true;
1198 v4l2_event_queue_fh(&inst->fh, &ev);
1199 wake_up(&inst->reconf_wait);
1201 mutex_unlock(&inst->lock);
1204 static void vdec_event_notify(struct venus_inst *inst, u32 event,
1205 struct hfi_event_data *data)
1207 struct venus_core *core = inst->core;
1208 struct device *dev = core->dev_dec;
1211 case EVT_SESSION_ERROR:
1212 inst->session_error = true;
1213 dev_err(dev, "dec: event session error %x\n", inst->error);
1215 case EVT_SYS_EVENT_CHANGE:
1216 switch (data->event_type) {
1217 case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
1218 vdec_event_change(inst, data, true);
1220 case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
1221 vdec_event_change(inst, data, false);
1223 case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
1224 venus_helper_release_buf_ref(inst, data->tag);
1235 static const struct hfi_inst_ops vdec_hfi_ops = {
1236 .buf_done = vdec_buf_done,
1237 .event_notify = vdec_event_notify,
1240 static void vdec_inst_init(struct venus_inst *inst)
1242 inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1243 inst->fmt_out = &vdec_formats[6];
1244 inst->fmt_cap = &vdec_formats[0];
1245 inst->width = frame_width_min(inst);
1246 inst->height = ALIGN(frame_height_min(inst), 32);
1247 inst->out_width = frame_width_min(inst);
1248 inst->out_height = frame_height_min(inst);
1250 inst->timeperframe.numerator = 1;
1251 inst->timeperframe.denominator = 30;
1252 inst->opb_buftype = HFI_BUFFER_OUTPUT;
1255 static void vdec_m2m_device_run(void *priv)
1259 static const struct v4l2_m2m_ops vdec_m2m_ops = {
1260 .device_run = vdec_m2m_device_run,
1261 .job_abort = venus_helper_m2m_job_abort,
1264 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1265 struct vb2_queue *dst_vq)
1267 struct venus_inst *inst = priv;
1270 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1271 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1272 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1273 src_vq->ops = &vdec_vb2_ops;
1274 src_vq->mem_ops = &vb2_dma_sg_memops;
1275 src_vq->drv_priv = inst;
1276 src_vq->buf_struct_size = sizeof(struct venus_buffer);
1277 src_vq->allow_zero_bytesused = 1;
1278 src_vq->min_buffers_needed = 0;
1279 src_vq->dev = inst->core->dev;
1280 ret = vb2_queue_init(src_vq);
1284 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1285 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1286 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1287 dst_vq->ops = &vdec_vb2_ops;
1288 dst_vq->mem_ops = &vb2_dma_sg_memops;
1289 dst_vq->drv_priv = inst;
1290 dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1291 dst_vq->allow_zero_bytesused = 1;
1292 dst_vq->min_buffers_needed = 0;
1293 dst_vq->dev = inst->core->dev;
1294 ret = vb2_queue_init(dst_vq);
1296 vb2_queue_release(src_vq);
1303 static int vdec_open(struct file *file)
1305 struct venus_core *core = video_drvdata(file);
1306 struct venus_inst *inst;
1309 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1313 INIT_LIST_HEAD(&inst->dpbbufs);
1314 INIT_LIST_HEAD(&inst->registeredbufs);
1315 INIT_LIST_HEAD(&inst->internalbufs);
1316 INIT_LIST_HEAD(&inst->list);
1317 mutex_init(&inst->lock);
1320 inst->session_type = VIDC_SESSION_TYPE_DEC;
1321 inst->num_output_bufs = 1;
1322 inst->codec_state = VENUS_DEC_STATE_DEINIT;
1323 inst->buf_count = 0;
1324 init_waitqueue_head(&inst->reconf_wait);
1325 venus_helper_init_instance(inst);
1327 ret = pm_runtime_get_sync(core->dev_dec);
1331 ret = vdec_ctrl_init(inst);
1335 ret = hfi_session_create(inst, &vdec_hfi_ops);
1337 goto err_ctrl_deinit;
1339 vdec_inst_init(inst);
1342 * create m2m device for every instance, the m2m context scheduling
1343 * is made by firmware side so we do not need to care about.
1345 inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1346 if (IS_ERR(inst->m2m_dev)) {
1347 ret = PTR_ERR(inst->m2m_dev);
1348 goto err_session_destroy;
1351 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1352 if (IS_ERR(inst->m2m_ctx)) {
1353 ret = PTR_ERR(inst->m2m_ctx);
1354 goto err_m2m_release;
1357 v4l2_fh_init(&inst->fh, core->vdev_dec);
1359 inst->fh.ctrl_handler = &inst->ctrl_handler;
1360 v4l2_fh_add(&inst->fh);
1361 inst->fh.m2m_ctx = inst->m2m_ctx;
1362 file->private_data = &inst->fh;
1367 v4l2_m2m_release(inst->m2m_dev);
1368 err_session_destroy:
1369 hfi_session_destroy(inst);
1371 vdec_ctrl_deinit(inst);
1373 pm_runtime_put_sync(core->dev_dec);
1379 static int vdec_close(struct file *file)
1381 struct venus_inst *inst = to_inst(file);
1383 v4l2_m2m_ctx_release(inst->m2m_ctx);
1384 v4l2_m2m_release(inst->m2m_dev);
1385 vdec_ctrl_deinit(inst);
1386 hfi_session_destroy(inst);
1387 mutex_destroy(&inst->lock);
1388 v4l2_fh_del(&inst->fh);
1389 v4l2_fh_exit(&inst->fh);
1391 pm_runtime_put_sync(inst->core->dev_dec);
1397 static const struct v4l2_file_operations vdec_fops = {
1398 .owner = THIS_MODULE,
1400 .release = vdec_close,
1401 .unlocked_ioctl = video_ioctl2,
1402 .poll = v4l2_m2m_fop_poll,
1403 .mmap = v4l2_m2m_fop_mmap,
1404 #ifdef CONFIG_COMPAT
1405 .compat_ioctl32 = v4l2_compat_ioctl32,
1409 static int vdec_probe(struct platform_device *pdev)
1411 struct device *dev = &pdev->dev;
1412 struct video_device *vdev;
1413 struct venus_core *core;
1417 return -EPROBE_DEFER;
1419 core = dev_get_drvdata(dev->parent);
1421 return -EPROBE_DEFER;
1423 if (IS_V3(core) || IS_V4(core)) {
1424 core->core0_clk = devm_clk_get(dev, "core");
1425 if (IS_ERR(core->core0_clk))
1426 return PTR_ERR(core->core0_clk);
1430 core->core0_bus_clk = devm_clk_get(dev, "bus");
1431 if (IS_ERR(core->core0_bus_clk))
1432 return PTR_ERR(core->core0_bus_clk);
1435 platform_set_drvdata(pdev, core);
1437 vdev = video_device_alloc();
1441 strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1442 vdev->release = video_device_release;
1443 vdev->fops = &vdec_fops;
1444 vdev->ioctl_ops = &vdec_ioctl_ops;
1445 vdev->vfl_dir = VFL_DIR_M2M;
1446 vdev->v4l2_dev = &core->v4l2_dev;
1447 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1449 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1451 goto err_vdev_release;
1453 core->vdev_dec = vdev;
1454 core->dev_dec = dev;
1456 video_set_drvdata(vdev, core);
1457 pm_runtime_enable(dev);
1462 video_device_release(vdev);
1466 static int vdec_remove(struct platform_device *pdev)
1468 struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1470 video_unregister_device(core->vdev_dec);
1471 pm_runtime_disable(core->dev_dec);
1476 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1478 struct venus_core *core = dev_get_drvdata(dev);
1484 ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true);
1489 clk_disable_unprepare(core->core0_bus_clk);
1491 clk_disable_unprepare(core->core0_clk);
1493 return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false);
1496 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1498 struct venus_core *core = dev_get_drvdata(dev);
1504 ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true);
1508 ret = clk_prepare_enable(core->core0_clk);
1510 goto err_power_disable;
1513 ret = clk_prepare_enable(core->core0_bus_clk);
1516 goto err_unprepare_core0;
1518 return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false);
1520 err_unprepare_core0:
1521 clk_disable_unprepare(core->core0_clk);
1523 venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false);
1527 static const struct dev_pm_ops vdec_pm_ops = {
1528 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1529 pm_runtime_force_resume)
1530 SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1533 static const struct of_device_id vdec_dt_match[] = {
1534 { .compatible = "venus-decoder" },
1537 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1539 static struct platform_driver qcom_venus_dec_driver = {
1540 .probe = vdec_probe,
1541 .remove = vdec_remove,
1543 .name = "qcom-venus-decoder",
1544 .of_match_table = vdec_dt_match,
1548 module_platform_driver(qcom_venus_dec_driver);
1550 MODULE_ALIAS("platform:qcom-venus-decoder");
1551 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1552 MODULE_LICENSE("GPL v2");