]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/platform/qcom/venus/vdec.c
media: venus: hfi_venus: add suspend functionality for Venus 4xx
[mirror_ubuntu-jammy-kernel.git] / drivers / media / platform / qcom / venus / vdec.c
CommitLineData
7472c1c6
SV
1/*
2 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2017 Linaro Ltd.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15#include <linux/clk.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/pm_runtime.h>
19#include <linux/slab.h>
20#include <media/v4l2-ioctl.h>
21#include <media/v4l2-event.h>
22#include <media/v4l2-ctrls.h>
23#include <media/v4l2-mem2mem.h>
24#include <media/videobuf2-dma-sg.h>
25
26#include "hfi_venus_io.h"
27#include "core.h"
28#include "helpers.h"
29#include "vdec.h"
30
31static u32 get_framesize_uncompressed(unsigned int plane, u32 width, u32 height)
32{
33 u32 y_stride, uv_stride, y_plane;
34 u32 y_sclines, uv_sclines, uv_plane;
35 u32 size;
36
37 y_stride = ALIGN(width, 128);
38 uv_stride = ALIGN(width, 128);
39 y_sclines = ALIGN(height, 32);
40 uv_sclines = ALIGN(((height + 1) >> 1), 16);
41
42 y_plane = y_stride * y_sclines;
43 uv_plane = uv_stride * uv_sclines + SZ_4K;
44 size = y_plane + uv_plane + SZ_8K;
45
46 return ALIGN(size, SZ_4K);
47}
48
49static u32 get_framesize_compressed(unsigned int width, unsigned int height)
50{
51 return ((width * height * 3 / 2) / 2) + 128;
52}
53
54/*
55 * Three resons to keep MPLANE formats (despite that the number of planes
56 * currently is one):
57 * - the MPLANE formats allow only one plane to be used
58 * - the downstream driver use MPLANE formats too
59 * - future firmware versions could add support for >1 planes
60 */
61static const struct venus_format vdec_formats[] = {
62 {
63 .pixfmt = V4L2_PIX_FMT_NV12,
64 .num_planes = 1,
65 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
66 }, {
67 .pixfmt = V4L2_PIX_FMT_MPEG4,
68 .num_planes = 1,
69 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
70 }, {
71 .pixfmt = V4L2_PIX_FMT_MPEG2,
72 .num_planes = 1,
73 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
74 }, {
75 .pixfmt = V4L2_PIX_FMT_H263,
76 .num_planes = 1,
77 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
78 }, {
79 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
80 .num_planes = 1,
81 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
82 }, {
83 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
84 .num_planes = 1,
85 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
86 }, {
87 .pixfmt = V4L2_PIX_FMT_H264,
88 .num_planes = 1,
89 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
90 }, {
91 .pixfmt = V4L2_PIX_FMT_VP8,
92 .num_planes = 1,
93 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
94 }, {
95 .pixfmt = V4L2_PIX_FMT_VP9,
96 .num_planes = 1,
97 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
98 }, {
99 .pixfmt = V4L2_PIX_FMT_XVID,
100 .num_planes = 1,
101 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
102 },
103};
104
29f0133e
SV
105static const struct venus_format *
106find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
7472c1c6
SV
107{
108 const struct venus_format *fmt = vdec_formats;
109 unsigned int size = ARRAY_SIZE(vdec_formats);
110 unsigned int i;
111
112 for (i = 0; i < size; i++) {
113 if (fmt[i].pixfmt == pixfmt)
114 break;
115 }
116
117 if (i == size || fmt[i].type != type)
118 return NULL;
119
29f0133e
SV
120 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
121 !venus_helper_check_codec(inst, fmt[i].pixfmt))
122 return NULL;
123
7472c1c6
SV
124 return &fmt[i];
125}
126
127static const struct venus_format *
29f0133e 128find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
7472c1c6
SV
129{
130 const struct venus_format *fmt = vdec_formats;
131 unsigned int size = ARRAY_SIZE(vdec_formats);
132 unsigned int i, k = 0;
133
134 if (index > size)
135 return NULL;
136
137 for (i = 0; i < size; i++) {
82e071e2
AC
138 bool valid;
139
7472c1c6
SV
140 if (fmt[i].type != type)
141 continue;
82e071e2
AC
142 valid = type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
143 venus_helper_check_codec(inst, fmt[i].pixfmt);
144 if (k == index && valid)
7472c1c6 145 break;
82e071e2
AC
146 if (valid)
147 k++;
7472c1c6
SV
148 }
149
150 if (i == size)
151 return NULL;
152
153 return &fmt[i];
154}
155
156static const struct venus_format *
157vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
158{
159 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
160 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
161 const struct venus_format *fmt;
162 unsigned int p;
163
164 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
165 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
166
29f0133e 167 fmt = find_format(inst, pixmp->pixelformat, f->type);
7472c1c6
SV
168 if (!fmt) {
169 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
170 pixmp->pixelformat = V4L2_PIX_FMT_NV12;
171 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
172 pixmp->pixelformat = V4L2_PIX_FMT_H264;
173 else
174 return NULL;
29f0133e 175 fmt = find_format(inst, pixmp->pixelformat, f->type);
7472c1c6
SV
176 }
177
178 pixmp->width = clamp(pixmp->width, inst->cap_width.min,
179 inst->cap_width.max);
180 pixmp->height = clamp(pixmp->height, inst->cap_height.min,
181 inst->cap_height.max);
182
183 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
184 pixmp->height = ALIGN(pixmp->height, 32);
185
186 if (pixmp->field == V4L2_FIELD_ANY)
187 pixmp->field = V4L2_FIELD_NONE;
188 pixmp->num_planes = fmt->num_planes;
189 pixmp->flags = 0;
190
191 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
192 for (p = 0; p < pixmp->num_planes; p++) {
193 pfmt[p].sizeimage =
194 get_framesize_uncompressed(p, pixmp->width,
195 pixmp->height);
196 pfmt[p].bytesperline = ALIGN(pixmp->width, 128);
197 }
198 } else {
199 pfmt[0].sizeimage = get_framesize_compressed(pixmp->width,
200 pixmp->height);
201 pfmt[0].bytesperline = 0;
202 }
203
204 return fmt;
205}
206
207static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
208{
209 struct venus_inst *inst = to_inst(file);
210
211 vdec_try_fmt_common(inst, f);
212
213 return 0;
214}
215
216static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
217{
218 struct venus_inst *inst = to_inst(file);
219 const struct venus_format *fmt = NULL;
220 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
221
222 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
223 fmt = inst->fmt_cap;
224 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
225 fmt = inst->fmt_out;
226
227 if (inst->reconfig) {
228 struct v4l2_format format = {};
229
230 inst->out_width = inst->reconfig_width;
231 inst->out_height = inst->reconfig_height;
232 inst->reconfig = false;
233
234 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
235 format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
236 format.fmt.pix_mp.width = inst->out_width;
237 format.fmt.pix_mp.height = inst->out_height;
238
239 vdec_try_fmt_common(inst, &format);
240
241 inst->width = format.fmt.pix_mp.width;
242 inst->height = format.fmt.pix_mp.height;
243 }
244
245 pixmp->pixelformat = fmt->pixfmt;
246
247 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
248 pixmp->width = inst->width;
249 pixmp->height = inst->height;
250 pixmp->colorspace = inst->colorspace;
251 pixmp->ycbcr_enc = inst->ycbcr_enc;
252 pixmp->quantization = inst->quantization;
253 pixmp->xfer_func = inst->xfer_func;
254 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
255 pixmp->width = inst->out_width;
256 pixmp->height = inst->out_height;
257 }
258
259 vdec_try_fmt_common(inst, f);
260
261 return 0;
262}
263
264static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
265{
266 struct venus_inst *inst = to_inst(file);
267 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
268 struct v4l2_pix_format_mplane orig_pixmp;
269 const struct venus_format *fmt;
270 struct v4l2_format format;
271 u32 pixfmt_out = 0, pixfmt_cap = 0;
272
273 orig_pixmp = *pixmp;
274
275 fmt = vdec_try_fmt_common(inst, f);
276
277 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
278 pixfmt_out = pixmp->pixelformat;
279 pixfmt_cap = inst->fmt_cap->pixfmt;
280 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
281 pixfmt_cap = pixmp->pixelformat;
282 pixfmt_out = inst->fmt_out->pixfmt;
283 }
284
285 memset(&format, 0, sizeof(format));
286
287 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
288 format.fmt.pix_mp.pixelformat = pixfmt_out;
289 format.fmt.pix_mp.width = orig_pixmp.width;
290 format.fmt.pix_mp.height = orig_pixmp.height;
291 vdec_try_fmt_common(inst, &format);
292
293 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
294 inst->out_width = format.fmt.pix_mp.width;
295 inst->out_height = format.fmt.pix_mp.height;
296 inst->colorspace = pixmp->colorspace;
297 inst->ycbcr_enc = pixmp->ycbcr_enc;
298 inst->quantization = pixmp->quantization;
299 inst->xfer_func = pixmp->xfer_func;
300 }
301
302 memset(&format, 0, sizeof(format));
303
304 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
305 format.fmt.pix_mp.pixelformat = pixfmt_cap;
306 format.fmt.pix_mp.width = orig_pixmp.width;
307 format.fmt.pix_mp.height = orig_pixmp.height;
308 vdec_try_fmt_common(inst, &format);
309
310 inst->width = format.fmt.pix_mp.width;
311 inst->height = format.fmt.pix_mp.height;
312
313 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
314 inst->fmt_out = fmt;
315 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
316 inst->fmt_cap = fmt;
317
318 return 0;
319}
320
321static int
322vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
323{
324 struct venus_inst *inst = to_inst(file);
325
326 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
327 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
328 return -EINVAL;
329
330 switch (s->target) {
331 case V4L2_SEL_TGT_CROP_BOUNDS:
332 case V4L2_SEL_TGT_CROP_DEFAULT:
333 case V4L2_SEL_TGT_CROP:
334 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
335 return -EINVAL;
336 s->r.width = inst->out_width;
337 s->r.height = inst->out_height;
338 break;
339 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
340 case V4L2_SEL_TGT_COMPOSE_PADDED:
341 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
342 return -EINVAL;
343 s->r.width = inst->width;
344 s->r.height = inst->height;
345 break;
346 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
347 case V4L2_SEL_TGT_COMPOSE:
348 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
349 return -EINVAL;
350 s->r.width = inst->out_width;
351 s->r.height = inst->out_height;
352 break;
353 default:
354 return -EINVAL;
355 }
356
357 s->r.top = 0;
358 s->r.left = 0;
359
360 return 0;
361}
362
363static int
364vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
365{
366 strlcpy(cap->driver, "qcom-venus", sizeof(cap->driver));
367 strlcpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
368 strlcpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
369
370 return 0;
371}
372
373static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
374{
29f0133e 375 struct venus_inst *inst = to_inst(file);
7472c1c6
SV
376 const struct venus_format *fmt;
377
378 memset(f->reserved, 0, sizeof(f->reserved));
379
29f0133e 380 fmt = find_format_by_index(inst, f->index, f->type);
7472c1c6
SV
381 if (!fmt)
382 return -EINVAL;
383
384 f->pixelformat = fmt->pixfmt;
385
386 return 0;
387}
388
389static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
390{
391 struct venus_inst *inst = to_inst(file);
392 struct v4l2_captureparm *cap = &a->parm.capture;
393 struct v4l2_fract *timeperframe = &cap->timeperframe;
394 u64 us_per_frame, fps;
395
396 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
397 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
398 return -EINVAL;
399
400 memset(cap->reserved, 0, sizeof(cap->reserved));
401 if (!timeperframe->denominator)
402 timeperframe->denominator = inst->timeperframe.denominator;
403 if (!timeperframe->numerator)
404 timeperframe->numerator = inst->timeperframe.numerator;
405 cap->readbuffers = 0;
406 cap->extendedmode = 0;
407 cap->capability = V4L2_CAP_TIMEPERFRAME;
408 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
409 do_div(us_per_frame, timeperframe->denominator);
410
411 if (!us_per_frame)
412 return -EINVAL;
413
414 fps = (u64)USEC_PER_SEC;
415 do_div(fps, us_per_frame);
416
417 inst->fps = fps;
418 inst->timeperframe = *timeperframe;
419
420 return 0;
421}
422
423static int vdec_enum_framesizes(struct file *file, void *fh,
424 struct v4l2_frmsizeenum *fsize)
425{
426 struct venus_inst *inst = to_inst(file);
427 const struct venus_format *fmt;
428
29f0133e 429 fmt = find_format(inst, fsize->pixel_format,
7472c1c6
SV
430 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
431 if (!fmt) {
29f0133e 432 fmt = find_format(inst, fsize->pixel_format,
7472c1c6
SV
433 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
434 if (!fmt)
435 return -EINVAL;
436 }
437
438 if (fsize->index)
439 return -EINVAL;
440
441 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
442
443 fsize->stepwise.min_width = inst->cap_width.min;
444 fsize->stepwise.max_width = inst->cap_width.max;
445 fsize->stepwise.step_width = inst->cap_width.step_size;
446 fsize->stepwise.min_height = inst->cap_height.min;
447 fsize->stepwise.max_height = inst->cap_height.max;
448 fsize->stepwise.step_height = inst->cap_height.step_size;
449
450 return 0;
451}
452
453static int vdec_subscribe_event(struct v4l2_fh *fh,
454 const struct v4l2_event_subscription *sub)
455{
456 switch (sub->type) {
457 case V4L2_EVENT_EOS:
458 return v4l2_event_subscribe(fh, sub, 2, NULL);
459 case V4L2_EVENT_SOURCE_CHANGE:
460 return v4l2_src_change_event_subscribe(fh, sub);
461 case V4L2_EVENT_CTRL:
462 return v4l2_ctrl_subscribe_event(fh, sub);
463 default:
464 return -EINVAL;
465 }
466}
467
468static int
469vdec_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
470{
e69b987a
SV
471 switch (cmd->cmd) {
472 case V4L2_DEC_CMD_STOP:
473 if (cmd->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
474 return -EINVAL;
475 break;
476 default:
7472c1c6 477 return -EINVAL;
e69b987a 478 }
7472c1c6
SV
479
480 return 0;
481}
482
483static int
484vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
485{
486 struct venus_inst *inst = to_inst(file);
e69b987a 487 struct hfi_frame_data fdata = {0};
7472c1c6
SV
488 int ret;
489
490 ret = vdec_try_decoder_cmd(file, fh, cmd);
491 if (ret)
492 return ret;
493
494 mutex_lock(&inst->lock);
7472c1c6 495
e69b987a
SV
496 /*
497 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on decoder
498 * input to signal EOS.
499 */
500 if (!(inst->streamon_out & inst->streamon_cap))
501 goto unlock;
7472c1c6 502
e69b987a
SV
503 fdata.buffer_type = HFI_BUFFER_INPUT;
504 fdata.flags |= HFI_BUFFERFLAG_EOS;
505 fdata.device_addr = 0xdeadbeef;
506
507 ret = hfi_session_process_buf(inst, &fdata);
508
509unlock:
510 mutex_unlock(&inst->lock);
511 return ret;
7472c1c6
SV
512}
513
514static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
515 .vidioc_querycap = vdec_querycap,
516 .vidioc_enum_fmt_vid_cap_mplane = vdec_enum_fmt,
517 .vidioc_enum_fmt_vid_out_mplane = vdec_enum_fmt,
518 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
519 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
520 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
521 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
522 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
523 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
524 .vidioc_g_selection = vdec_g_selection,
525 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
526 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
527 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
528 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
529 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
530 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
531 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
532 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
533 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
534 .vidioc_s_parm = vdec_s_parm,
535 .vidioc_enum_framesizes = vdec_enum_framesizes,
536 .vidioc_subscribe_event = vdec_subscribe_event,
537 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
538 .vidioc_try_decoder_cmd = vdec_try_decoder_cmd,
539 .vidioc_decoder_cmd = vdec_decoder_cmd,
540};
541
542static int vdec_set_properties(struct venus_inst *inst)
543{
544 struct vdec_controls *ctr = &inst->controls.dec;
545 struct venus_core *core = inst->core;
546 struct hfi_enable en = { .enable = 1 };
547 u32 ptype;
548 int ret;
549
550 if (core->res->hfi_version == HFI_VERSION_1XX) {
551 ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
552 ret = hfi_session_set_property(inst, ptype, &en);
553 if (ret)
554 return ret;
555 }
556
557 if (core->res->hfi_version == HFI_VERSION_3XX ||
558 inst->cap_bufs_mode_dynamic) {
559 struct hfi_buffer_alloc_mode mode;
560
561 ptype = HFI_PROPERTY_PARAM_BUFFER_ALLOC_MODE;
562 mode.type = HFI_BUFFER_OUTPUT;
563 mode.mode = HFI_BUFFER_MODE_DYNAMIC;
564
565 ret = hfi_session_set_property(inst, ptype, &mode);
566 if (ret)
567 return ret;
568 }
569
570 if (ctr->post_loop_deb_mode) {
571 ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
572 en.enable = 1;
573 ret = hfi_session_set_property(inst, ptype, &en);
574 if (ret)
575 return ret;
576 }
577
578 return 0;
579}
580
581static int vdec_init_session(struct venus_inst *inst)
582{
583 int ret;
584
585 ret = hfi_session_init(inst, inst->fmt_out->pixfmt);
586 if (ret)
587 return ret;
588
589 ret = venus_helper_set_input_resolution(inst, inst->out_width,
590 inst->out_height);
591 if (ret)
592 goto deinit;
593
594 ret = venus_helper_set_color_format(inst, inst->fmt_cap->pixfmt);
595 if (ret)
596 goto deinit;
597
598 return 0;
599deinit:
600 hfi_session_deinit(inst);
601 return ret;
602}
603
604static int vdec_cap_num_buffers(struct venus_inst *inst, unsigned int *num)
605{
606 struct hfi_buffer_requirements bufreq;
607 int ret;
608
609 ret = vdec_init_session(inst);
610 if (ret)
611 return ret;
612
613 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
614
615 *num = bufreq.count_actual;
616
617 hfi_session_deinit(inst);
618
619 return ret;
620}
621
622static int vdec_queue_setup(struct vb2_queue *q,
623 unsigned int *num_buffers, unsigned int *num_planes,
624 unsigned int sizes[], struct device *alloc_devs[])
625{
626 struct venus_inst *inst = vb2_get_drv_priv(q);
627 unsigned int p, num;
628 int ret = 0;
629
630 if (*num_planes) {
631 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
632 *num_planes != inst->fmt_out->num_planes)
633 return -EINVAL;
634
635 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
636 *num_planes != inst->fmt_cap->num_planes)
637 return -EINVAL;
638
639 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
640 sizes[0] < inst->input_buf_size)
641 return -EINVAL;
642
643 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
644 sizes[0] < inst->output_buf_size)
645 return -EINVAL;
646
647 return 0;
648 }
649
650 switch (q->type) {
651 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
652 *num_planes = inst->fmt_out->num_planes;
653 sizes[0] = get_framesize_compressed(inst->out_width,
654 inst->out_height);
655 inst->input_buf_size = sizes[0];
656 inst->num_input_bufs = *num_buffers;
ebebc593
SV
657
658 ret = vdec_cap_num_buffers(inst, &num);
659 if (ret)
660 break;
661
662 inst->num_output_bufs = num;
7472c1c6
SV
663 break;
664 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
665 *num_planes = inst->fmt_cap->num_planes;
666
667 ret = vdec_cap_num_buffers(inst, &num);
668 if (ret)
669 break;
670
671 *num_buffers = max(*num_buffers, num);
672
673 for (p = 0; p < *num_planes; p++)
674 sizes[p] = get_framesize_uncompressed(p, inst->width,
675 inst->height);
676
677 inst->num_output_bufs = *num_buffers;
678 inst->output_buf_size = sizes[0];
679 break;
680 default:
681 ret = -EINVAL;
682 break;
683 }
684
685 return ret;
686}
687
688static int vdec_verify_conf(struct venus_inst *inst)
689{
f04997bd 690 enum hfi_version ver = inst->core->res->hfi_version;
7472c1c6
SV
691 struct hfi_buffer_requirements bufreq;
692 int ret;
693
694 if (!inst->num_input_bufs || !inst->num_output_bufs)
695 return -EINVAL;
696
697 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
698 if (ret)
699 return ret;
700
701 if (inst->num_output_bufs < bufreq.count_actual ||
f04997bd 702 inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
7472c1c6
SV
703 return -EINVAL;
704
705 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
706 if (ret)
707 return ret;
708
f04997bd 709 if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
7472c1c6
SV
710 return -EINVAL;
711
712 return 0;
713}
714
715static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
716{
717 struct venus_inst *inst = vb2_get_drv_priv(q);
718 struct venus_core *core = inst->core;
719 u32 ptype;
720 int ret;
721
722 mutex_lock(&inst->lock);
723
724 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
725 inst->streamon_out = 1;
726 else
727 inst->streamon_cap = 1;
728
729 if (!(inst->streamon_out & inst->streamon_cap)) {
730 mutex_unlock(&inst->lock);
731 return 0;
732 }
733
734 venus_helper_init_instance(inst);
735
736 inst->reconfig = false;
737 inst->sequence_cap = 0;
738 inst->sequence_out = 0;
7472c1c6
SV
739
740 ret = vdec_init_session(inst);
741 if (ret)
742 goto bufs_done;
743
744 ret = vdec_set_properties(inst);
745 if (ret)
746 goto deinit_sess;
747
748 if (core->res->hfi_version == HFI_VERSION_3XX) {
749 struct hfi_buffer_size_actual buf_sz;
750
751 ptype = HFI_PROPERTY_PARAM_BUFFER_SIZE_ACTUAL;
752 buf_sz.type = HFI_BUFFER_OUTPUT;
753 buf_sz.size = inst->output_buf_size;
754
755 ret = hfi_session_set_property(inst, ptype, &buf_sz);
756 if (ret)
757 goto deinit_sess;
758 }
759
760 ret = vdec_verify_conf(inst);
761 if (ret)
762 goto deinit_sess;
763
764 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
765 VB2_MAX_FRAME);
766 if (ret)
767 goto deinit_sess;
768
769 ret = venus_helper_vb2_start_streaming(inst);
770 if (ret)
771 goto deinit_sess;
772
773 mutex_unlock(&inst->lock);
774
775 return 0;
776
777deinit_sess:
778 hfi_session_deinit(inst);
779bufs_done:
780 venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
781 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
782 inst->streamon_out = 0;
783 else
784 inst->streamon_cap = 0;
785 mutex_unlock(&inst->lock);
786 return ret;
787}
788
789static const struct vb2_ops vdec_vb2_ops = {
790 .queue_setup = vdec_queue_setup,
791 .buf_init = venus_helper_vb2_buf_init,
792 .buf_prepare = venus_helper_vb2_buf_prepare,
793 .start_streaming = vdec_start_streaming,
794 .stop_streaming = venus_helper_vb2_stop_streaming,
795 .buf_queue = venus_helper_vb2_buf_queue,
796};
797
798static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
799 u32 tag, u32 bytesused, u32 data_offset, u32 flags,
800 u32 hfi_flags, u64 timestamp_us)
801{
802 enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
803 struct vb2_v4l2_buffer *vbuf;
804 struct vb2_buffer *vb;
805 unsigned int type;
806
807 if (buf_type == HFI_BUFFER_INPUT)
808 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
809 else
810 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
811
812 vbuf = venus_helper_find_buf(inst, type, tag);
813 if (!vbuf)
814 return;
815
816 vbuf->flags = flags;
817 vbuf->field = V4L2_FIELD_NONE;
818
819 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
820 vb = &vbuf->vb2_buf;
821 vb->planes[0].bytesused =
822 max_t(unsigned int, inst->output_buf_size, bytesused);
823 vb->planes[0].data_offset = data_offset;
824 vb->timestamp = timestamp_us * NSEC_PER_USEC;
825 vbuf->sequence = inst->sequence_cap++;
826
7472c1c6
SV
827 if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
828 const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
829
830 v4l2_event_queue_fh(&inst->fh, &ev);
831 }
832 } else {
833 vbuf->sequence = inst->sequence_out++;
834 }
835
836 if (hfi_flags & HFI_BUFFERFLAG_READONLY)
837 venus_helper_acquire_buf_ref(vbuf);
838
839 if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
840 state = VB2_BUF_STATE_ERROR;
841
842 v4l2_m2m_buf_done(vbuf, state);
843}
844
845static void vdec_event_notify(struct venus_inst *inst, u32 event,
846 struct hfi_event_data *data)
847{
848 struct venus_core *core = inst->core;
849 struct device *dev = core->dev_dec;
850 static const struct v4l2_event ev = {
851 .type = V4L2_EVENT_SOURCE_CHANGE,
852 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
853
854 switch (event) {
855 case EVT_SESSION_ERROR:
856 inst->session_error = true;
857 dev_err(dev, "dec: event session error %x\n", inst->error);
858 break;
859 case EVT_SYS_EVENT_CHANGE:
860 switch (data->event_type) {
861 case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
862 hfi_session_continue(inst);
863 dev_dbg(dev, "event sufficient resources\n");
864 break;
865 case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
866 inst->reconfig_height = data->height;
867 inst->reconfig_width = data->width;
868 inst->reconfig = true;
869
870 v4l2_event_queue_fh(&inst->fh, &ev);
871
872 dev_dbg(dev, "event not sufficient resources (%ux%u)\n",
873 data->width, data->height);
874 break;
875 case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
876 venus_helper_release_buf_ref(inst, data->tag);
877 break;
878 default:
879 break;
880 }
881 break;
882 default:
883 break;
884 }
885}
886
887static const struct hfi_inst_ops vdec_hfi_ops = {
888 .buf_done = vdec_buf_done,
889 .event_notify = vdec_event_notify,
890};
891
892static void vdec_inst_init(struct venus_inst *inst)
893{
894 inst->fmt_out = &vdec_formats[6];
895 inst->fmt_cap = &vdec_formats[0];
896 inst->width = 1280;
897 inst->height = ALIGN(720, 32);
898 inst->out_width = 1280;
899 inst->out_height = 720;
900 inst->fps = 30;
901 inst->timeperframe.numerator = 1;
902 inst->timeperframe.denominator = 30;
903
904 inst->cap_width.min = 64;
905 inst->cap_width.max = 1920;
906 if (inst->core->res->hfi_version == HFI_VERSION_3XX)
907 inst->cap_width.max = 3840;
908 inst->cap_width.step_size = 1;
909 inst->cap_height.min = 64;
910 inst->cap_height.max = ALIGN(1080, 32);
911 if (inst->core->res->hfi_version == HFI_VERSION_3XX)
912 inst->cap_height.max = ALIGN(2160, 32);
913 inst->cap_height.step_size = 1;
914 inst->cap_framerate.min = 1;
915 inst->cap_framerate.max = 30;
916 inst->cap_framerate.step_size = 1;
917 inst->cap_mbs_per_frame.min = 16;
918 inst->cap_mbs_per_frame.max = 8160;
919}
920
921static const struct v4l2_m2m_ops vdec_m2m_ops = {
922 .device_run = venus_helper_m2m_device_run,
923 .job_abort = venus_helper_m2m_job_abort,
924};
925
926static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
927 struct vb2_queue *dst_vq)
928{
929 struct venus_inst *inst = priv;
930 int ret;
931
932 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
933 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
934 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
935 src_vq->ops = &vdec_vb2_ops;
936 src_vq->mem_ops = &vb2_dma_sg_memops;
937 src_vq->drv_priv = inst;
938 src_vq->buf_struct_size = sizeof(struct venus_buffer);
939 src_vq->allow_zero_bytesused = 1;
940 src_vq->min_buffers_needed = 1;
941 src_vq->dev = inst->core->dev;
942 ret = vb2_queue_init(src_vq);
943 if (ret)
944 return ret;
945
946 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
947 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
948 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
949 dst_vq->ops = &vdec_vb2_ops;
950 dst_vq->mem_ops = &vb2_dma_sg_memops;
951 dst_vq->drv_priv = inst;
952 dst_vq->buf_struct_size = sizeof(struct venus_buffer);
953 dst_vq->allow_zero_bytesused = 1;
954 dst_vq->min_buffers_needed = 1;
955 dst_vq->dev = inst->core->dev;
956 ret = vb2_queue_init(dst_vq);
957 if (ret) {
958 vb2_queue_release(src_vq);
959 return ret;
960 }
961
962 return 0;
963}
964
965static int vdec_open(struct file *file)
966{
967 struct venus_core *core = video_drvdata(file);
968 struct venus_inst *inst;
969 int ret;
970
971 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
972 if (!inst)
973 return -ENOMEM;
974
975 INIT_LIST_HEAD(&inst->registeredbufs);
976 INIT_LIST_HEAD(&inst->internalbufs);
977 INIT_LIST_HEAD(&inst->list);
978 mutex_init(&inst->lock);
979
980 inst->core = core;
981 inst->session_type = VIDC_SESSION_TYPE_DEC;
ebebc593 982 inst->num_output_bufs = 1;
7472c1c6
SV
983
984 venus_helper_init_instance(inst);
985
986 ret = pm_runtime_get_sync(core->dev_dec);
987 if (ret < 0)
988 goto err_free_inst;
989
990 ret = vdec_ctrl_init(inst);
991 if (ret)
992 goto err_put_sync;
993
994 ret = hfi_session_create(inst, &vdec_hfi_ops);
995 if (ret)
996 goto err_ctrl_deinit;
997
998 vdec_inst_init(inst);
999
1000 /*
1001 * create m2m device for every instance, the m2m context scheduling
1002 * is made by firmware side so we do not need to care about.
1003 */
1004 inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1005 if (IS_ERR(inst->m2m_dev)) {
1006 ret = PTR_ERR(inst->m2m_dev);
1007 goto err_session_destroy;
1008 }
1009
1010 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1011 if (IS_ERR(inst->m2m_ctx)) {
1012 ret = PTR_ERR(inst->m2m_ctx);
1013 goto err_m2m_release;
1014 }
1015
1016 v4l2_fh_init(&inst->fh, core->vdev_dec);
1017
1018 inst->fh.ctrl_handler = &inst->ctrl_handler;
1019 v4l2_fh_add(&inst->fh);
1020 inst->fh.m2m_ctx = inst->m2m_ctx;
1021 file->private_data = &inst->fh;
1022
1023 return 0;
1024
1025err_m2m_release:
1026 v4l2_m2m_release(inst->m2m_dev);
1027err_session_destroy:
1028 hfi_session_destroy(inst);
1029err_ctrl_deinit:
1030 vdec_ctrl_deinit(inst);
1031err_put_sync:
1032 pm_runtime_put_sync(core->dev_dec);
1033err_free_inst:
1034 kfree(inst);
1035 return ret;
1036}
1037
1038static int vdec_close(struct file *file)
1039{
1040 struct venus_inst *inst = to_inst(file);
1041
1042 v4l2_m2m_ctx_release(inst->m2m_ctx);
1043 v4l2_m2m_release(inst->m2m_dev);
1044 vdec_ctrl_deinit(inst);
1045 hfi_session_destroy(inst);
1046 mutex_destroy(&inst->lock);
1047 v4l2_fh_del(&inst->fh);
1048 v4l2_fh_exit(&inst->fh);
7472c1c6
SV
1049
1050 pm_runtime_put_sync(inst->core->dev_dec);
17571ed6
SV
1051
1052 kfree(inst);
7472c1c6
SV
1053 return 0;
1054}
1055
1056static const struct v4l2_file_operations vdec_fops = {
1057 .owner = THIS_MODULE,
1058 .open = vdec_open,
1059 .release = vdec_close,
1060 .unlocked_ioctl = video_ioctl2,
1061 .poll = v4l2_m2m_fop_poll,
1062 .mmap = v4l2_m2m_fop_mmap,
1063#ifdef CONFIG_COMPAT
1064 .compat_ioctl32 = v4l2_compat_ioctl32,
1065#endif
1066};
1067
1068static int vdec_probe(struct platform_device *pdev)
1069{
1070 struct device *dev = &pdev->dev;
1071 struct video_device *vdev;
1072 struct venus_core *core;
1073 int ret;
1074
1075 if (!dev->parent)
1076 return -EPROBE_DEFER;
1077
1078 core = dev_get_drvdata(dev->parent);
1079 if (!core)
1080 return -EPROBE_DEFER;
1081
1082 if (core->res->hfi_version == HFI_VERSION_3XX) {
1083 core->core0_clk = devm_clk_get(dev, "core");
1084 if (IS_ERR(core->core0_clk))
1085 return PTR_ERR(core->core0_clk);
1086 }
1087
1088 platform_set_drvdata(pdev, core);
1089
1090 vdev = video_device_alloc();
1091 if (!vdev)
1092 return -ENOMEM;
1093
5c2c1659 1094 strlcpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
7472c1c6
SV
1095 vdev->release = video_device_release;
1096 vdev->fops = &vdec_fops;
1097 vdev->ioctl_ops = &vdec_ioctl_ops;
1098 vdev->vfl_dir = VFL_DIR_M2M;
1099 vdev->v4l2_dev = &core->v4l2_dev;
1100 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1101
1102 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1103 if (ret)
1104 goto err_vdev_release;
1105
1106 core->vdev_dec = vdev;
1107 core->dev_dec = dev;
1108
1109 video_set_drvdata(vdev, core);
1110 pm_runtime_enable(dev);
1111
1112 return 0;
1113
1114err_vdev_release:
1115 video_device_release(vdev);
1116 return ret;
1117}
1118
1119static int vdec_remove(struct platform_device *pdev)
1120{
1121 struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1122
1123 video_unregister_device(core->vdev_dec);
1124 pm_runtime_disable(core->dev_dec);
1125
1126 return 0;
1127}
1128
62d625c9 1129static __maybe_unused int vdec_runtime_suspend(struct device *dev)
7472c1c6
SV
1130{
1131 struct venus_core *core = dev_get_drvdata(dev);
1132
1133 if (core->res->hfi_version == HFI_VERSION_1XX)
1134 return 0;
1135
1136 writel(0, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1137 clk_disable_unprepare(core->core0_clk);
1138 writel(1, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1139
1140 return 0;
1141}
1142
62d625c9 1143static __maybe_unused int vdec_runtime_resume(struct device *dev)
7472c1c6
SV
1144{
1145 struct venus_core *core = dev_get_drvdata(dev);
1146 int ret;
1147
1148 if (core->res->hfi_version == HFI_VERSION_1XX)
1149 return 0;
1150
1151 writel(0, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1152 ret = clk_prepare_enable(core->core0_clk);
1153 writel(1, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1154
1155 return ret;
1156}
7472c1c6
SV
1157
1158static const struct dev_pm_ops vdec_pm_ops = {
1159 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1160 pm_runtime_force_resume)
1161 SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1162};
1163
1164static const struct of_device_id vdec_dt_match[] = {
1165 { .compatible = "venus-decoder" },
1166 { }
1167};
1168MODULE_DEVICE_TABLE(of, vdec_dt_match);
1169
1170static struct platform_driver qcom_venus_dec_driver = {
1171 .probe = vdec_probe,
1172 .remove = vdec_remove,
1173 .driver = {
1174 .name = "qcom-venus-decoder",
1175 .of_match_table = vdec_dt_match,
1176 .pm = &vdec_pm_ops,
1177 },
1178};
1179module_platform_driver(qcom_venus_dec_driver);
1180
1181MODULE_ALIAS("platform:qcom-venus-decoder");
1182MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1183MODULE_LICENSE("GPL v2");