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"
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_NV12,
36 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
38 .pixfmt = V4L2_PIX_FMT_MPEG4,
40 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
41 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
43 .pixfmt = V4L2_PIX_FMT_MPEG2,
45 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
46 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
48 .pixfmt = V4L2_PIX_FMT_H263,
50 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
51 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
53 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
55 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
56 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
58 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
60 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
61 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
63 .pixfmt = V4L2_PIX_FMT_H264,
65 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
66 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
68 .pixfmt = V4L2_PIX_FMT_VP8,
70 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
71 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
73 .pixfmt = V4L2_PIX_FMT_VP9,
75 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
76 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
78 .pixfmt = V4L2_PIX_FMT_XVID,
80 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
81 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
83 .pixfmt = V4L2_PIX_FMT_HEVC,
85 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
86 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
90 static const struct venus_format *
91 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
93 const struct venus_format *fmt = vdec_formats;
94 unsigned int size = ARRAY_SIZE(vdec_formats);
97 for (i = 0; i < size; i++) {
98 if (fmt[i].pixfmt == pixfmt)
102 if (i == size || fmt[i].type != type)
105 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
106 !venus_helper_check_codec(inst, fmt[i].pixfmt))
112 static const struct venus_format *
113 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
115 const struct venus_format *fmt = vdec_formats;
116 unsigned int size = ARRAY_SIZE(vdec_formats);
117 unsigned int i, k = 0;
122 for (i = 0; i < size; i++) {
125 if (fmt[i].type != type)
127 valid = type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
128 venus_helper_check_codec(inst, fmt[i].pixfmt);
129 if (k == index && valid)
141 static const struct venus_format *
142 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
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;
149 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
150 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
152 fmt = find_format(inst, pixmp->pixelformat, f->type);
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;
160 fmt = find_format(inst, pixmp->pixelformat, f->type);
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));
168 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
169 pixmp->height = ALIGN(pixmp->height, 32);
171 if (pixmp->field == V4L2_FIELD_ANY)
172 pixmp->field = V4L2_FIELD_NONE;
173 pixmp->num_planes = fmt->num_planes;
176 szimage = venus_helper_get_framesz(pixmp->pixelformat, pixmp->width,
179 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
180 pfmt[0].sizeimage = szimage;
181 pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
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;
191 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
193 struct venus_inst *inst = to_inst(file);
195 vdec_try_fmt_common(inst, f);
200 static int vdec_check_src_change(struct venus_inst *inst)
204 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE &&
205 inst->codec_state == VENUS_DEC_STATE_INIT &&
209 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE)
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.
218 if (inst->codec_state != VENUS_DEC_STATE_INIT)
221 ret = wait_event_timeout(inst->reconf_wait, inst->reconfig,
222 msecs_to_jiffies(100));
226 if (!(inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) ||
228 dev_dbg(inst->core->dev, "%s: wrong state\n", __func__);
234 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
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;
241 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
243 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
246 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
247 ret = vdec_check_src_change(inst);
252 pixmp->pixelformat = fmt->pixfmt;
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;
266 vdec_try_fmt_common(inst, f);
271 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
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;
282 fmt = vdec_try_fmt_common(inst, f);
284 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
285 pixfmt_out = pixmp->pixelformat;
286 pixfmt_cap = inst->fmt_cap->pixfmt;
287 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
288 pixfmt_cap = pixmp->pixelformat;
289 pixfmt_out = inst->fmt_out->pixfmt;
292 memset(&format, 0, sizeof(format));
294 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
295 format.fmt.pix_mp.pixelformat = pixfmt_out;
296 format.fmt.pix_mp.width = orig_pixmp.width;
297 format.fmt.pix_mp.height = orig_pixmp.height;
298 vdec_try_fmt_common(inst, &format);
300 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
301 inst->out_width = format.fmt.pix_mp.width;
302 inst->out_height = format.fmt.pix_mp.height;
303 inst->colorspace = pixmp->colorspace;
304 inst->ycbcr_enc = pixmp->ycbcr_enc;
305 inst->quantization = pixmp->quantization;
306 inst->xfer_func = pixmp->xfer_func;
307 inst->input_buf_size = pixmp->plane_fmt[0].sizeimage;
310 memset(&format, 0, sizeof(format));
312 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
313 format.fmt.pix_mp.pixelformat = pixfmt_cap;
314 format.fmt.pix_mp.width = orig_pixmp.width;
315 format.fmt.pix_mp.height = orig_pixmp.height;
316 vdec_try_fmt_common(inst, &format);
318 inst->width = format.fmt.pix_mp.width;
319 inst->height = format.fmt.pix_mp.height;
321 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
323 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
330 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
332 struct venus_inst *inst = to_inst(file);
334 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
335 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
339 case V4L2_SEL_TGT_CROP_BOUNDS:
340 case V4L2_SEL_TGT_CROP_DEFAULT:
341 case V4L2_SEL_TGT_CROP:
342 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
344 s->r.width = inst->out_width;
345 s->r.height = inst->out_height;
347 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
348 case V4L2_SEL_TGT_COMPOSE_PADDED:
349 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
351 s->r.width = inst->width;
352 s->r.height = inst->height;
354 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
355 case V4L2_SEL_TGT_COMPOSE:
356 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
358 s->r.width = inst->out_width;
359 s->r.height = inst->out_height;
372 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
374 strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
375 strscpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
376 strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
381 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
383 struct venus_inst *inst = to_inst(file);
384 const struct venus_format *fmt;
386 memset(f->reserved, 0, sizeof(f->reserved));
388 fmt = find_format_by_index(inst, f->index, f->type);
392 f->pixelformat = fmt->pixfmt;
393 f->flags = fmt->flags;
398 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
400 struct venus_inst *inst = to_inst(file);
401 struct v4l2_captureparm *cap = &a->parm.capture;
402 struct v4l2_fract *timeperframe = &cap->timeperframe;
403 u64 us_per_frame, fps;
405 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
406 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
409 memset(cap->reserved, 0, sizeof(cap->reserved));
410 if (!timeperframe->denominator)
411 timeperframe->denominator = inst->timeperframe.denominator;
412 if (!timeperframe->numerator)
413 timeperframe->numerator = inst->timeperframe.numerator;
414 cap->readbuffers = 0;
415 cap->extendedmode = 0;
416 cap->capability = V4L2_CAP_TIMEPERFRAME;
417 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
418 do_div(us_per_frame, timeperframe->denominator);
423 fps = (u64)USEC_PER_SEC;
424 do_div(fps, us_per_frame);
427 inst->timeperframe = *timeperframe;
432 static int vdec_enum_framesizes(struct file *file, void *fh,
433 struct v4l2_frmsizeenum *fsize)
435 struct venus_inst *inst = to_inst(file);
436 const struct venus_format *fmt;
438 fmt = find_format(inst, fsize->pixel_format,
439 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
441 fmt = find_format(inst, fsize->pixel_format,
442 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
450 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
452 fsize->stepwise.min_width = frame_width_min(inst);
453 fsize->stepwise.max_width = frame_width_max(inst);
454 fsize->stepwise.step_width = frame_width_step(inst);
455 fsize->stepwise.min_height = frame_height_min(inst);
456 fsize->stepwise.max_height = frame_height_max(inst);
457 fsize->stepwise.step_height = frame_height_step(inst);
462 static int vdec_subscribe_event(struct v4l2_fh *fh,
463 const struct v4l2_event_subscription *sub)
465 struct venus_inst *inst = container_of(fh, struct venus_inst, fh);
470 return v4l2_event_subscribe(fh, sub, 2, NULL);
471 case V4L2_EVENT_SOURCE_CHANGE:
472 ret = v4l2_src_change_event_subscribe(fh, sub);
475 inst->subscriptions |= V4L2_EVENT_SOURCE_CHANGE;
477 case V4L2_EVENT_CTRL:
478 return v4l2_ctrl_subscribe_event(fh, sub);
485 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
487 struct venus_inst *inst = to_inst(file);
488 struct hfi_frame_data fdata = {0};
491 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
495 mutex_lock(&inst->lock);
497 if (cmd->cmd == V4L2_DEC_CMD_STOP) {
499 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on
500 * decoder input to signal EOS.
502 if (!(inst->streamon_out && inst->streamon_cap))
505 fdata.buffer_type = HFI_BUFFER_INPUT;
506 fdata.flags |= HFI_BUFFERFLAG_EOS;
507 fdata.device_addr = 0xdeadb000;
509 ret = hfi_session_process_buf(inst, &fdata);
511 if (!ret && inst->codec_state == VENUS_DEC_STATE_DECODING)
512 inst->codec_state = VENUS_DEC_STATE_DRAIN;
516 mutex_unlock(&inst->lock);
520 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
521 .vidioc_querycap = vdec_querycap,
522 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
523 .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
524 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
525 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
526 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
527 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
528 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
529 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
530 .vidioc_g_selection = vdec_g_selection,
531 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
532 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
533 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
534 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
535 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
536 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
537 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
538 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
539 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
540 .vidioc_s_parm = vdec_s_parm,
541 .vidioc_enum_framesizes = vdec_enum_framesizes,
542 .vidioc_subscribe_event = vdec_subscribe_event,
543 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
544 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
545 .vidioc_decoder_cmd = vdec_decoder_cmd,
548 static int vdec_set_properties(struct venus_inst *inst)
550 struct vdec_controls *ctr = &inst->controls.dec;
551 struct hfi_enable en = { .enable = 1 };
555 if (ctr->post_loop_deb_mode) {
556 ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
557 ret = hfi_session_set_property(inst, ptype, &en);
565 #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE))
567 static int vdec_output_conf(struct venus_inst *inst)
569 struct venus_core *core = inst->core;
570 struct hfi_enable en = { .enable = 1 };
571 u32 width = inst->out_width;
572 u32 height = inst->out_height;
573 u32 out_fmt, out2_fmt;
578 ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
582 if (core->res->hfi_version == HFI_VERSION_1XX) {
583 ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
584 ret = hfi_session_set_property(inst, ptype, &en);
589 /* Force searching UBWC formats for bigger then HD resolutions */
590 if (width > 1920 && height > ALIGN(1080, 32))
593 /* For Venus v4 UBWC format is mandatory */
597 ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt,
602 inst->output_buf_size =
603 venus_helper_get_framesz_raw(out_fmt, width, height);
604 inst->output2_buf_size =
605 venus_helper_get_framesz_raw(out2_fmt, width, height);
607 if (is_ubwc_fmt(out_fmt)) {
608 inst->opb_buftype = HFI_BUFFER_OUTPUT2;
609 inst->opb_fmt = out2_fmt;
610 inst->dpb_buftype = HFI_BUFFER_OUTPUT;
611 inst->dpb_fmt = out_fmt;
612 } else if (is_ubwc_fmt(out2_fmt)) {
613 inst->opb_buftype = HFI_BUFFER_OUTPUT;
614 inst->opb_fmt = out_fmt;
615 inst->dpb_buftype = HFI_BUFFER_OUTPUT2;
616 inst->dpb_fmt = out2_fmt;
618 inst->opb_buftype = HFI_BUFFER_OUTPUT;
619 inst->opb_fmt = out_fmt;
620 inst->dpb_buftype = 0;
624 ret = venus_helper_set_raw_format(inst, inst->opb_fmt,
630 ret = venus_helper_set_multistream(inst, false, true);
634 ret = venus_helper_set_raw_format(inst, inst->dpb_fmt,
639 ret = venus_helper_set_output_resolution(inst, width, height,
645 if (IS_V3(core) || IS_V4(core)) {
646 if (inst->output2_buf_size) {
647 ret = venus_helper_set_bufsize(inst,
648 inst->output2_buf_size,
654 if (inst->output_buf_size) {
655 ret = venus_helper_set_bufsize(inst,
656 inst->output_buf_size,
663 ret = venus_helper_set_dyn_bufmode(inst);
670 static int vdec_session_init(struct venus_inst *inst)
674 ret = hfi_session_init(inst, inst->fmt_out->pixfmt);
680 ret = venus_helper_set_input_resolution(inst, frame_width_min(inst),
681 frame_height_min(inst));
685 ret = venus_helper_init_codec_freq_data(inst);
691 hfi_session_deinit(inst);
695 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num,
696 unsigned int *out_num)
698 enum hfi_version ver = inst->core->res->hfi_version;
699 struct hfi_buffer_requirements bufreq;
702 *in_num = *out_num = 0;
704 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
708 *in_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
710 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
714 *out_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
719 static int vdec_queue_setup(struct vb2_queue *q,
720 unsigned int *num_buffers, unsigned int *num_planes,
721 unsigned int sizes[], struct device *alloc_devs[])
723 struct venus_inst *inst = vb2_get_drv_priv(q);
724 unsigned int in_num, out_num;
728 unsigned int output_buf_size = venus_helper_get_opb_size(inst);
730 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
731 *num_planes != inst->fmt_out->num_planes)
734 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
735 *num_planes != inst->fmt_cap->num_planes)
738 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
739 sizes[0] < inst->input_buf_size)
742 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
743 sizes[0] < output_buf_size)
749 ret = vdec_session_init(inst);
753 ret = vdec_num_buffers(inst, &in_num, &out_num);
758 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
759 *num_planes = inst->fmt_out->num_planes;
760 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
763 sizes[0] = max(sizes[0], inst->input_buf_size);
764 inst->input_buf_size = sizes[0];
765 *num_buffers = max(*num_buffers, in_num);
766 inst->num_input_bufs = *num_buffers;
767 inst->num_output_bufs = out_num;
769 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
770 *num_planes = inst->fmt_cap->num_planes;
771 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
774 inst->output_buf_size = sizes[0];
775 *num_buffers = max(*num_buffers, out_num);
776 inst->num_output_bufs = *num_buffers;
778 mutex_lock(&inst->lock);
779 if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP)
780 inst->codec_state = VENUS_DEC_STATE_STOPPED;
781 mutex_unlock(&inst->lock);
791 static int vdec_verify_conf(struct venus_inst *inst)
793 enum hfi_version ver = inst->core->res->hfi_version;
794 struct hfi_buffer_requirements bufreq;
797 if (!inst->num_input_bufs || !inst->num_output_bufs)
800 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
804 if (inst->num_output_bufs < bufreq.count_actual ||
805 inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
808 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
812 if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
818 static int vdec_start_capture(struct venus_inst *inst)
822 if (!inst->streamon_out)
825 if (inst->codec_state == VENUS_DEC_STATE_DECODING) {
829 venus_helper_queue_dpb_bufs(inst);
830 venus_helper_process_initial_cap_bufs(inst);
831 inst->streamon_cap = 1;
835 if (inst->codec_state != VENUS_DEC_STATE_STOPPED)
839 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT);
843 ret = vdec_output_conf(inst);
847 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
848 VB2_MAX_FRAME, VB2_MAX_FRAME);
852 ret = venus_helper_intbufs_realloc(inst);
856 ret = venus_helper_alloc_dpb_bufs(inst);
860 ret = venus_helper_queue_dpb_bufs(inst);
864 ret = venus_helper_process_initial_cap_bufs(inst);
868 venus_pm_load_scale(inst);
870 ret = hfi_session_continue(inst);
874 inst->codec_state = VENUS_DEC_STATE_DECODING;
876 inst->streamon_cap = 1;
877 inst->sequence_cap = 0;
878 inst->reconfig = false;
883 venus_helper_free_dpb_bufs(inst);
888 static int vdec_start_output(struct venus_inst *inst)
892 if (inst->codec_state == VENUS_DEC_STATE_SEEK) {
893 ret = venus_helper_process_initial_out_bufs(inst);
894 inst->codec_state = VENUS_DEC_STATE_DECODING;
898 if (inst->codec_state == VENUS_DEC_STATE_INIT ||
899 inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) {
900 ret = venus_helper_process_initial_out_bufs(inst);
904 if (inst->codec_state != VENUS_DEC_STATE_DEINIT)
907 venus_helper_init_instance(inst);
908 inst->sequence_out = 0;
909 inst->reconfig = false;
911 ret = vdec_set_properties(inst);
915 ret = vdec_output_conf(inst);
919 ret = vdec_verify_conf(inst);
923 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
924 VB2_MAX_FRAME, VB2_MAX_FRAME);
928 ret = venus_helper_vb2_start_streaming(inst);
932 ret = venus_helper_process_initial_out_bufs(inst);
936 inst->codec_state = VENUS_DEC_STATE_INIT;
939 inst->streamon_out = 1;
943 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
945 struct venus_inst *inst = vb2_get_drv_priv(q);
948 mutex_lock(&inst->lock);
950 ret = venus_pm_acquire_core(inst);
954 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
955 ret = vdec_start_capture(inst);
957 ret = vdec_start_output(inst);
962 mutex_unlock(&inst->lock);
966 venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
967 mutex_unlock(&inst->lock);
971 static void vdec_cancel_dst_buffers(struct venus_inst *inst)
973 struct vb2_v4l2_buffer *buf;
975 while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
976 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
979 static int vdec_stop_capture(struct venus_inst *inst)
983 switch (inst->codec_state) {
984 case VENUS_DEC_STATE_DECODING:
985 ret = hfi_session_flush(inst, HFI_FLUSH_ALL);
987 case VENUS_DEC_STATE_DRAIN:
988 vdec_cancel_dst_buffers(inst);
989 inst->codec_state = VENUS_DEC_STATE_STOPPED;
991 case VENUS_DEC_STATE_DRC:
992 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT);
993 vdec_cancel_dst_buffers(inst);
994 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
995 INIT_LIST_HEAD(&inst->registeredbufs);
996 venus_helper_free_dpb_bufs(inst);
1005 static int vdec_stop_output(struct venus_inst *inst)
1009 switch (inst->codec_state) {
1010 case VENUS_DEC_STATE_DECODING:
1011 case VENUS_DEC_STATE_DRAIN:
1012 case VENUS_DEC_STATE_STOPPED:
1013 ret = hfi_session_flush(inst, HFI_FLUSH_ALL);
1014 inst->codec_state = VENUS_DEC_STATE_SEEK;
1016 case VENUS_DEC_STATE_INIT:
1017 case VENUS_DEC_STATE_CAPTURE_SETUP:
1018 ret = hfi_session_flush(inst, HFI_FLUSH_INPUT);
1027 static void vdec_stop_streaming(struct vb2_queue *q)
1029 struct venus_inst *inst = vb2_get_drv_priv(q);
1032 mutex_lock(&inst->lock);
1034 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1035 ret = vdec_stop_capture(inst);
1037 ret = vdec_stop_output(inst);
1039 venus_helper_buffers_done(inst, VB2_BUF_STATE_ERROR);
1044 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1045 inst->streamon_out = 0;
1047 inst->streamon_cap = 0;
1050 mutex_unlock(&inst->lock);
1053 static void vdec_session_release(struct venus_inst *inst)
1055 struct venus_core *core = inst->core;
1058 mutex_lock(&inst->lock);
1060 inst->codec_state = VENUS_DEC_STATE_DEINIT;
1062 ret = hfi_session_stop(inst);
1063 abort = (ret && ret != -EINVAL) ? 1 : 0;
1064 ret = hfi_session_unload_res(inst);
1065 abort = (ret && ret != -EINVAL) ? 1 : 0;
1066 ret = venus_helper_unregister_bufs(inst);
1067 abort = (ret && ret != -EINVAL) ? 1 : 0;
1068 ret = venus_helper_intbufs_free(inst);
1069 abort = (ret && ret != -EINVAL) ? 1 : 0;
1070 ret = hfi_session_deinit(inst);
1071 abort = (ret && ret != -EINVAL) ? 1 : 0;
1073 if (inst->session_error || core->sys_error)
1077 hfi_session_abort(inst);
1079 venus_helper_free_dpb_bufs(inst);
1080 venus_pm_load_scale(inst);
1081 venus_pm_release_core(inst);
1082 INIT_LIST_HEAD(&inst->registeredbufs);
1084 mutex_unlock(&inst->lock);
1087 static int vdec_buf_init(struct vb2_buffer *vb)
1089 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1093 return venus_helper_vb2_buf_init(vb);
1096 static void vdec_buf_cleanup(struct vb2_buffer *vb)
1098 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1101 if (!inst->buf_count)
1102 vdec_session_release(inst);
1105 static const struct vb2_ops vdec_vb2_ops = {
1106 .queue_setup = vdec_queue_setup,
1107 .buf_init = vdec_buf_init,
1108 .buf_cleanup = vdec_buf_cleanup,
1109 .buf_prepare = venus_helper_vb2_buf_prepare,
1110 .start_streaming = vdec_start_streaming,
1111 .stop_streaming = vdec_stop_streaming,
1112 .buf_queue = venus_helper_vb2_buf_queue,
1115 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
1116 u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1117 u32 hfi_flags, u64 timestamp_us)
1119 enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1120 struct vb2_v4l2_buffer *vbuf;
1121 struct vb2_buffer *vb;
1124 if (buf_type == HFI_BUFFER_INPUT)
1125 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1127 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1129 vbuf = venus_helper_find_buf(inst, type, tag);
1133 vbuf->flags = flags;
1134 vbuf->field = V4L2_FIELD_NONE;
1135 vb = &vbuf->vb2_buf;
1137 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1138 vb2_set_plane_payload(vb, 0, bytesused);
1139 vb->planes[0].data_offset = data_offset;
1140 vb->timestamp = timestamp_us * NSEC_PER_USEC;
1141 vbuf->sequence = inst->sequence_cap++;
1143 if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
1144 const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
1146 v4l2_event_queue_fh(&inst->fh, &ev);
1148 if (inst->codec_state == VENUS_DEC_STATE_DRAIN)
1149 inst->codec_state = VENUS_DEC_STATE_STOPPED;
1152 vbuf->sequence = inst->sequence_out++;
1155 venus_helper_get_ts_metadata(inst, timestamp_us, vbuf);
1157 if (hfi_flags & HFI_BUFFERFLAG_READONLY)
1158 venus_helper_acquire_buf_ref(vbuf);
1160 if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
1161 state = VB2_BUF_STATE_ERROR;
1163 if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) {
1164 state = VB2_BUF_STATE_ERROR;
1165 vb2_set_plane_payload(vb, 0, 0);
1169 v4l2_m2m_buf_done(vbuf, state);
1172 static void vdec_event_change(struct venus_inst *inst,
1173 struct hfi_event_data *ev_data, bool sufficient)
1175 static const struct v4l2_event ev = {
1176 .type = V4L2_EVENT_SOURCE_CHANGE,
1177 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
1178 struct device *dev = inst->core->dev_dec;
1179 struct v4l2_format format = {};
1181 mutex_lock(&inst->lock);
1183 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1184 format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
1185 format.fmt.pix_mp.width = ev_data->width;
1186 format.fmt.pix_mp.height = ev_data->height;
1188 vdec_try_fmt_common(inst, &format);
1190 inst->width = format.fmt.pix_mp.width;
1191 inst->height = format.fmt.pix_mp.height;
1193 inst->out_width = ev_data->width;
1194 inst->out_height = ev_data->height;
1196 if (inst->bit_depth != ev_data->bit_depth)
1197 inst->bit_depth = ev_data->bit_depth;
1199 dev_dbg(dev, "event %s sufficient resources (%ux%u)\n",
1200 sufficient ? "" : "not", ev_data->width, ev_data->height);
1203 hfi_session_continue(inst);
1205 switch (inst->codec_state) {
1206 case VENUS_DEC_STATE_INIT:
1207 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1209 case VENUS_DEC_STATE_DECODING:
1210 inst->codec_state = VENUS_DEC_STATE_DRC;
1217 inst->reconfig = true;
1218 v4l2_event_queue_fh(&inst->fh, &ev);
1219 wake_up(&inst->reconf_wait);
1221 mutex_unlock(&inst->lock);
1224 static void vdec_event_notify(struct venus_inst *inst, u32 event,
1225 struct hfi_event_data *data)
1227 struct venus_core *core = inst->core;
1228 struct device *dev = core->dev_dec;
1231 case EVT_SESSION_ERROR:
1232 inst->session_error = true;
1233 dev_err(dev, "dec: event session error %x\n", inst->error);
1235 case EVT_SYS_EVENT_CHANGE:
1236 switch (data->event_type) {
1237 case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
1238 vdec_event_change(inst, data, true);
1240 case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
1241 vdec_event_change(inst, data, false);
1243 case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
1244 venus_helper_release_buf_ref(inst, data->tag);
1255 static const struct hfi_inst_ops vdec_hfi_ops = {
1256 .buf_done = vdec_buf_done,
1257 .event_notify = vdec_event_notify,
1260 static void vdec_inst_init(struct venus_inst *inst)
1262 inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1263 inst->fmt_out = &vdec_formats[6];
1264 inst->fmt_cap = &vdec_formats[0];
1265 inst->width = frame_width_min(inst);
1266 inst->height = ALIGN(frame_height_min(inst), 32);
1267 inst->out_width = frame_width_min(inst);
1268 inst->out_height = frame_height_min(inst);
1270 inst->timeperframe.numerator = 1;
1271 inst->timeperframe.denominator = 30;
1272 inst->opb_buftype = HFI_BUFFER_OUTPUT;
1275 static void vdec_m2m_device_run(void *priv)
1279 static const struct v4l2_m2m_ops vdec_m2m_ops = {
1280 .device_run = vdec_m2m_device_run,
1281 .job_abort = venus_helper_m2m_job_abort,
1284 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1285 struct vb2_queue *dst_vq)
1287 struct venus_inst *inst = priv;
1290 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1291 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1292 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1293 src_vq->ops = &vdec_vb2_ops;
1294 src_vq->mem_ops = &vb2_dma_sg_memops;
1295 src_vq->drv_priv = inst;
1296 src_vq->buf_struct_size = sizeof(struct venus_buffer);
1297 src_vq->allow_zero_bytesused = 1;
1298 src_vq->min_buffers_needed = 0;
1299 src_vq->dev = inst->core->dev;
1300 ret = vb2_queue_init(src_vq);
1304 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1305 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1306 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1307 dst_vq->ops = &vdec_vb2_ops;
1308 dst_vq->mem_ops = &vb2_dma_sg_memops;
1309 dst_vq->drv_priv = inst;
1310 dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1311 dst_vq->allow_zero_bytesused = 1;
1312 dst_vq->min_buffers_needed = 0;
1313 dst_vq->dev = inst->core->dev;
1314 ret = vb2_queue_init(dst_vq);
1316 vb2_queue_release(src_vq);
1323 static int vdec_open(struct file *file)
1325 struct venus_core *core = video_drvdata(file);
1326 struct venus_inst *inst;
1329 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1333 INIT_LIST_HEAD(&inst->dpbbufs);
1334 INIT_LIST_HEAD(&inst->registeredbufs);
1335 INIT_LIST_HEAD(&inst->internalbufs);
1336 INIT_LIST_HEAD(&inst->list);
1337 mutex_init(&inst->lock);
1340 inst->session_type = VIDC_SESSION_TYPE_DEC;
1341 inst->num_output_bufs = 1;
1342 inst->codec_state = VENUS_DEC_STATE_DEINIT;
1343 inst->buf_count = 0;
1344 inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1345 inst->core_acquired = false;
1346 inst->bit_depth = VIDC_BITDEPTH_8;
1347 init_waitqueue_head(&inst->reconf_wait);
1348 venus_helper_init_instance(inst);
1350 ret = pm_runtime_get_sync(core->dev_dec);
1354 ret = vdec_ctrl_init(inst);
1358 ret = hfi_session_create(inst, &vdec_hfi_ops);
1360 goto err_ctrl_deinit;
1362 vdec_inst_init(inst);
1365 * create m2m device for every instance, the m2m context scheduling
1366 * is made by firmware side so we do not need to care about.
1368 inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1369 if (IS_ERR(inst->m2m_dev)) {
1370 ret = PTR_ERR(inst->m2m_dev);
1371 goto err_session_destroy;
1374 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1375 if (IS_ERR(inst->m2m_ctx)) {
1376 ret = PTR_ERR(inst->m2m_ctx);
1377 goto err_m2m_release;
1380 v4l2_fh_init(&inst->fh, core->vdev_dec);
1382 inst->fh.ctrl_handler = &inst->ctrl_handler;
1383 v4l2_fh_add(&inst->fh);
1384 inst->fh.m2m_ctx = inst->m2m_ctx;
1385 file->private_data = &inst->fh;
1390 v4l2_m2m_release(inst->m2m_dev);
1391 err_session_destroy:
1392 hfi_session_destroy(inst);
1394 vdec_ctrl_deinit(inst);
1396 pm_runtime_put_sync(core->dev_dec);
1402 static int vdec_close(struct file *file)
1404 struct venus_inst *inst = to_inst(file);
1406 v4l2_m2m_ctx_release(inst->m2m_ctx);
1407 v4l2_m2m_release(inst->m2m_dev);
1408 vdec_ctrl_deinit(inst);
1409 hfi_session_destroy(inst);
1410 mutex_destroy(&inst->lock);
1411 v4l2_fh_del(&inst->fh);
1412 v4l2_fh_exit(&inst->fh);
1414 pm_runtime_put_sync(inst->core->dev_dec);
1420 static const struct v4l2_file_operations vdec_fops = {
1421 .owner = THIS_MODULE,
1423 .release = vdec_close,
1424 .unlocked_ioctl = video_ioctl2,
1425 .poll = v4l2_m2m_fop_poll,
1426 .mmap = v4l2_m2m_fop_mmap,
1429 static int vdec_probe(struct platform_device *pdev)
1431 struct device *dev = &pdev->dev;
1432 struct video_device *vdev;
1433 struct venus_core *core;
1437 return -EPROBE_DEFER;
1439 core = dev_get_drvdata(dev->parent);
1441 return -EPROBE_DEFER;
1443 platform_set_drvdata(pdev, core);
1445 if (core->pm_ops->vdec_get) {
1446 ret = core->pm_ops->vdec_get(dev);
1451 vdev = video_device_alloc();
1455 strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1456 vdev->release = video_device_release;
1457 vdev->fops = &vdec_fops;
1458 vdev->ioctl_ops = &vdec_ioctl_ops;
1459 vdev->vfl_dir = VFL_DIR_M2M;
1460 vdev->v4l2_dev = &core->v4l2_dev;
1461 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1463 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1465 goto err_vdev_release;
1467 core->vdev_dec = vdev;
1468 core->dev_dec = dev;
1470 video_set_drvdata(vdev, core);
1471 pm_runtime_enable(dev);
1476 video_device_release(vdev);
1480 static int vdec_remove(struct platform_device *pdev)
1482 struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1484 video_unregister_device(core->vdev_dec);
1485 pm_runtime_disable(core->dev_dec);
1487 if (core->pm_ops->vdec_put)
1488 core->pm_ops->vdec_put(core->dev_dec);
1493 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1495 struct venus_core *core = dev_get_drvdata(dev);
1496 const struct venus_pm_ops *pm_ops = core->pm_ops;
1499 if (pm_ops->vdec_power)
1500 ret = pm_ops->vdec_power(dev, POWER_OFF);
1505 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1507 struct venus_core *core = dev_get_drvdata(dev);
1508 const struct venus_pm_ops *pm_ops = core->pm_ops;
1511 if (pm_ops->vdec_power)
1512 ret = pm_ops->vdec_power(dev, POWER_ON);
1517 static const struct dev_pm_ops vdec_pm_ops = {
1518 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1519 pm_runtime_force_resume)
1520 SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1523 static const struct of_device_id vdec_dt_match[] = {
1524 { .compatible = "venus-decoder" },
1527 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1529 static struct platform_driver qcom_venus_dec_driver = {
1530 .probe = vdec_probe,
1531 .remove = vdec_remove,
1533 .name = "qcom-venus-decoder",
1534 .of_match_table = vdec_dt_match,
1538 module_platform_driver(qcom_venus_dec_driver);
1540 MODULE_ALIAS("platform:qcom-venus-decoder");
1541 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1542 MODULE_LICENSE("GPL v2");