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/list.h>
8 #include <linux/mutex.h>
9 #include <linux/slab.h>
10 #include <linux/kernel.h>
11 #include <media/videobuf2-dma-contig.h>
12 #include <media/v4l2-mem2mem.h>
13 #include <asm/div64.h>
17 #include "hfi_helper.h"
18 #include "pm_helpers.h"
19 #include "hfi_platform.h"
20 #include "hfi_parser.h"
22 #define NUM_MBS_720P (((ALIGN(1280, 16)) >> 4) * ((ALIGN(736, 16)) >> 4))
23 #define NUM_MBS_4K (((ALIGN(4096, 16)) >> 4) * ((ALIGN(2304, 16)) >> 4))
31 struct list_head list;
37 enum dpb_buf_owner owned_by;
41 bool venus_helper_check_codec(struct venus_inst *inst, u32 v4l2_pixfmt)
43 struct venus_core *core = inst->core;
44 u32 session_type = inst->session_type;
47 switch (v4l2_pixfmt) {
48 case V4L2_PIX_FMT_H264:
49 codec = HFI_VIDEO_CODEC_H264;
51 case V4L2_PIX_FMT_H263:
52 codec = HFI_VIDEO_CODEC_H263;
54 case V4L2_PIX_FMT_MPEG1:
55 codec = HFI_VIDEO_CODEC_MPEG1;
57 case V4L2_PIX_FMT_MPEG2:
58 codec = HFI_VIDEO_CODEC_MPEG2;
60 case V4L2_PIX_FMT_MPEG4:
61 codec = HFI_VIDEO_CODEC_MPEG4;
63 case V4L2_PIX_FMT_VC1_ANNEX_G:
64 case V4L2_PIX_FMT_VC1_ANNEX_L:
65 codec = HFI_VIDEO_CODEC_VC1;
67 case V4L2_PIX_FMT_VP8:
68 codec = HFI_VIDEO_CODEC_VP8;
70 case V4L2_PIX_FMT_VP9:
71 codec = HFI_VIDEO_CODEC_VP9;
73 case V4L2_PIX_FMT_XVID:
74 codec = HFI_VIDEO_CODEC_DIVX;
76 case V4L2_PIX_FMT_HEVC:
77 codec = HFI_VIDEO_CODEC_HEVC;
83 if (session_type == VIDC_SESSION_TYPE_ENC && core->enc_codecs & codec)
86 if (session_type == VIDC_SESSION_TYPE_DEC && core->dec_codecs & codec)
91 EXPORT_SYMBOL_GPL(venus_helper_check_codec);
93 static void free_dpb_buf(struct venus_inst *inst, struct intbuf *buf)
95 ida_free(&inst->dpb_ids, buf->dpb_out_tag);
97 list_del_init(&buf->list);
98 dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
103 int venus_helper_queue_dpb_bufs(struct venus_inst *inst)
105 struct intbuf *buf, *next;
106 unsigned int dpb_size = 0;
109 if (inst->dpb_buftype == HFI_BUFFER_OUTPUT)
110 dpb_size = inst->output_buf_size;
111 else if (inst->dpb_buftype == HFI_BUFFER_OUTPUT2)
112 dpb_size = inst->output2_buf_size;
114 list_for_each_entry_safe(buf, next, &inst->dpbbufs, list) {
115 struct hfi_frame_data fdata;
117 memset(&fdata, 0, sizeof(fdata));
118 fdata.alloc_len = buf->size;
119 fdata.device_addr = buf->da;
120 fdata.buffer_type = buf->type;
122 if (buf->owned_by == FIRMWARE)
125 /* free buffer from previous sequence which was released later */
126 if (dpb_size > buf->size) {
127 free_dpb_buf(inst, buf);
131 fdata.clnt_data = buf->dpb_out_tag;
133 ret = hfi_session_process_buf(inst, &fdata);
137 buf->owned_by = FIRMWARE;
143 EXPORT_SYMBOL_GPL(venus_helper_queue_dpb_bufs);
145 int venus_helper_free_dpb_bufs(struct venus_inst *inst)
147 struct intbuf *buf, *n;
149 list_for_each_entry_safe(buf, n, &inst->dpbbufs, list) {
150 if (buf->owned_by == FIRMWARE)
152 free_dpb_buf(inst, buf);
155 if (list_empty(&inst->dpbbufs))
156 INIT_LIST_HEAD(&inst->dpbbufs);
160 EXPORT_SYMBOL_GPL(venus_helper_free_dpb_bufs);
162 int venus_helper_alloc_dpb_bufs(struct venus_inst *inst)
164 struct venus_core *core = inst->core;
165 struct device *dev = core->dev;
166 enum hfi_version ver = core->res->hfi_version;
167 struct hfi_buffer_requirements bufreq;
168 u32 buftype = inst->dpb_buftype;
169 unsigned int dpb_size = 0;
176 /* no need to allocate dpb buffers */
180 if (inst->dpb_buftype == HFI_BUFFER_OUTPUT)
181 dpb_size = inst->output_buf_size;
182 else if (inst->dpb_buftype == HFI_BUFFER_OUTPUT2)
183 dpb_size = inst->output2_buf_size;
188 ret = venus_helper_get_bufreq(inst, buftype, &bufreq);
192 count = hfi_bufreq_get_count_min(&bufreq, ver);
194 for (i = 0; i < count; i++) {
195 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
202 buf->size = dpb_size;
203 buf->attrs = DMA_ATTR_WRITE_COMBINE |
204 DMA_ATTR_NO_KERNEL_MAPPING;
205 buf->va = dma_alloc_attrs(dev, buf->size, &buf->da, GFP_KERNEL,
211 buf->owned_by = DRIVER;
213 id = ida_alloc_min(&inst->dpb_ids, VB2_MAX_FRAME, GFP_KERNEL);
219 buf->dpb_out_tag = id;
221 list_add_tail(&buf->list, &inst->dpbbufs);
228 venus_helper_free_dpb_bufs(inst);
231 EXPORT_SYMBOL_GPL(venus_helper_alloc_dpb_bufs);
233 static int intbufs_set_buffer(struct venus_inst *inst, u32 type)
235 struct venus_core *core = inst->core;
236 struct device *dev = core->dev;
237 struct hfi_buffer_requirements bufreq;
238 struct hfi_buffer_desc bd;
243 ret = venus_helper_get_bufreq(inst, type, &bufreq);
250 for (i = 0; i < bufreq.count_actual; i++) {
251 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
257 buf->type = bufreq.type;
258 buf->size = bufreq.size;
259 buf->attrs = DMA_ATTR_WRITE_COMBINE |
260 DMA_ATTR_NO_KERNEL_MAPPING;
261 buf->va = dma_alloc_attrs(dev, buf->size, &buf->da, GFP_KERNEL,
268 memset(&bd, 0, sizeof(bd));
269 bd.buffer_size = buf->size;
270 bd.buffer_type = buf->type;
272 bd.device_addr = buf->da;
274 ret = hfi_session_set_buffers(inst, &bd);
276 dev_err(dev, "set session buffers failed\n");
280 list_add_tail(&buf->list, &inst->internalbufs);
286 dma_free_attrs(dev, buf->size, buf->va, buf->da, buf->attrs);
292 static int intbufs_unset_buffers(struct venus_inst *inst)
294 struct hfi_buffer_desc bd = {0};
295 struct intbuf *buf, *n;
298 list_for_each_entry_safe(buf, n, &inst->internalbufs, list) {
299 bd.buffer_size = buf->size;
300 bd.buffer_type = buf->type;
302 bd.device_addr = buf->da;
303 bd.response_required = true;
305 ret = hfi_session_unset_buffers(inst, &bd);
307 list_del_init(&buf->list);
308 dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
316 static const unsigned int intbuf_types_1xx[] = {
317 HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_1XX),
318 HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_1XX),
319 HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_1XX),
320 HFI_BUFFER_INTERNAL_PERSIST,
321 HFI_BUFFER_INTERNAL_PERSIST_1,
324 static const unsigned int intbuf_types_4xx[] = {
325 HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_4XX),
326 HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_4XX),
327 HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_4XX),
328 HFI_BUFFER_INTERNAL_PERSIST,
329 HFI_BUFFER_INTERNAL_PERSIST_1,
332 static const unsigned int intbuf_types_6xx[] = {
333 HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_6XX),
334 HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_6XX),
335 HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_6XX),
336 HFI_BUFFER_INTERNAL_PERSIST,
337 HFI_BUFFER_INTERNAL_PERSIST_1,
340 int venus_helper_intbufs_alloc(struct venus_inst *inst)
342 const unsigned int *intbuf;
346 if (IS_V6(inst->core)) {
347 arr_sz = ARRAY_SIZE(intbuf_types_6xx);
348 intbuf = intbuf_types_6xx;
349 } else if (IS_V4(inst->core)) {
350 arr_sz = ARRAY_SIZE(intbuf_types_4xx);
351 intbuf = intbuf_types_4xx;
353 arr_sz = ARRAY_SIZE(intbuf_types_1xx);
354 intbuf = intbuf_types_1xx;
357 for (i = 0; i < arr_sz; i++) {
358 ret = intbufs_set_buffer(inst, intbuf[i]);
366 intbufs_unset_buffers(inst);
369 EXPORT_SYMBOL_GPL(venus_helper_intbufs_alloc);
371 int venus_helper_intbufs_free(struct venus_inst *inst)
373 return intbufs_unset_buffers(inst);
375 EXPORT_SYMBOL_GPL(venus_helper_intbufs_free);
377 int venus_helper_intbufs_realloc(struct venus_inst *inst)
379 enum hfi_version ver = inst->core->res->hfi_version;
380 struct hfi_buffer_desc bd;
381 struct intbuf *buf, *n;
384 list_for_each_entry_safe(buf, n, &inst->internalbufs, list) {
385 if (buf->type == HFI_BUFFER_INTERNAL_PERSIST ||
386 buf->type == HFI_BUFFER_INTERNAL_PERSIST_1)
389 memset(&bd, 0, sizeof(bd));
390 bd.buffer_size = buf->size;
391 bd.buffer_type = buf->type;
393 bd.device_addr = buf->da;
394 bd.response_required = true;
396 ret = hfi_session_unset_buffers(inst, &bd);
398 dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
401 list_del_init(&buf->list);
405 ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH(ver));
409 ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH_1(ver));
413 ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH_2(ver));
421 EXPORT_SYMBOL_GPL(venus_helper_intbufs_realloc);
423 static void fill_buffer_desc(const struct venus_buffer *buf,
424 struct hfi_buffer_desc *bd, bool response)
426 memset(bd, 0, sizeof(*bd));
427 bd->buffer_type = HFI_BUFFER_OUTPUT;
428 bd->buffer_size = buf->size;
430 bd->device_addr = buf->dma_addr;
431 bd->response_required = response;
434 static void return_buf_error(struct venus_inst *inst,
435 struct vb2_v4l2_buffer *vbuf)
437 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
439 if (vbuf->vb2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
440 v4l2_m2m_src_buf_remove_by_buf(m2m_ctx, vbuf);
442 v4l2_m2m_dst_buf_remove_by_buf(m2m_ctx, vbuf);
444 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
448 put_ts_metadata(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
450 struct vb2_buffer *vb = &vbuf->vb2_buf;
453 u64 ts_us = vb->timestamp;
455 for (i = 0; i < ARRAY_SIZE(inst->tss); i++) {
456 if (!inst->tss[i].used) {
463 dev_dbg(inst->core->dev, VDBGL "no free slot\n");
467 do_div(ts_us, NSEC_PER_USEC);
469 inst->tss[slot].used = true;
470 inst->tss[slot].flags = vbuf->flags;
471 inst->tss[slot].tc = vbuf->timecode;
472 inst->tss[slot].ts_us = ts_us;
473 inst->tss[slot].ts_ns = vb->timestamp;
476 void venus_helper_get_ts_metadata(struct venus_inst *inst, u64 timestamp_us,
477 struct vb2_v4l2_buffer *vbuf)
479 struct vb2_buffer *vb = &vbuf->vb2_buf;
482 for (i = 0; i < ARRAY_SIZE(inst->tss); ++i) {
483 if (!inst->tss[i].used)
486 if (inst->tss[i].ts_us != timestamp_us)
489 inst->tss[i].used = false;
490 vbuf->flags |= inst->tss[i].flags;
491 vbuf->timecode = inst->tss[i].tc;
492 vb->timestamp = inst->tss[i].ts_ns;
496 EXPORT_SYMBOL_GPL(venus_helper_get_ts_metadata);
499 session_process_buf(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
501 struct venus_buffer *buf = to_venus_buffer(vbuf);
502 struct vb2_buffer *vb = &vbuf->vb2_buf;
503 unsigned int type = vb->type;
504 struct hfi_frame_data fdata;
506 memset(&fdata, 0, sizeof(fdata));
507 fdata.alloc_len = buf->size;
508 fdata.device_addr = buf->dma_addr;
509 fdata.timestamp = vb->timestamp;
510 do_div(fdata.timestamp, NSEC_PER_USEC);
512 fdata.clnt_data = vbuf->vb2_buf.index;
514 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
515 fdata.buffer_type = HFI_BUFFER_INPUT;
516 fdata.filled_len = vb2_get_plane_payload(vb, 0);
517 fdata.offset = vb->planes[0].data_offset;
519 if (vbuf->flags & V4L2_BUF_FLAG_LAST || !fdata.filled_len)
520 fdata.flags |= HFI_BUFFERFLAG_EOS;
522 if (inst->session_type == VIDC_SESSION_TYPE_DEC)
523 put_ts_metadata(inst, vbuf);
525 venus_pm_load_scale(inst);
526 } else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
527 if (inst->session_type == VIDC_SESSION_TYPE_ENC)
528 fdata.buffer_type = HFI_BUFFER_OUTPUT;
530 fdata.buffer_type = inst->opb_buftype;
531 fdata.filled_len = 0;
535 return hfi_session_process_buf(inst, &fdata);
538 static bool is_dynamic_bufmode(struct venus_inst *inst)
540 struct venus_core *core = inst->core;
541 struct hfi_plat_caps *caps;
544 * v4 doesn't send BUFFER_ALLOC_MODE_SUPPORTED property and supports
545 * dynamic buffer mode by default for HFI_BUFFER_OUTPUT/OUTPUT2.
547 if (IS_V4(core) || IS_V6(core))
550 caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
554 return caps->cap_bufs_mode_dynamic;
557 int venus_helper_unregister_bufs(struct venus_inst *inst)
559 struct venus_buffer *buf, *n;
560 struct hfi_buffer_desc bd;
563 if (is_dynamic_bufmode(inst))
566 list_for_each_entry_safe(buf, n, &inst->registeredbufs, reg_list) {
567 fill_buffer_desc(buf, &bd, true);
568 ret = hfi_session_unset_buffers(inst, &bd);
569 list_del_init(&buf->reg_list);
574 EXPORT_SYMBOL_GPL(venus_helper_unregister_bufs);
576 static int session_register_bufs(struct venus_inst *inst)
578 struct venus_core *core = inst->core;
579 struct device *dev = core->dev;
580 struct hfi_buffer_desc bd;
581 struct venus_buffer *buf;
584 if (is_dynamic_bufmode(inst))
587 list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
588 fill_buffer_desc(buf, &bd, false);
589 ret = hfi_session_set_buffers(inst, &bd);
591 dev_err(dev, "%s: set buffer failed\n", __func__);
599 static u32 to_hfi_raw_fmt(u32 v4l2_fmt)
602 case V4L2_PIX_FMT_NV12:
603 return HFI_COLOR_FORMAT_NV12;
604 case V4L2_PIX_FMT_NV21:
605 return HFI_COLOR_FORMAT_NV21;
606 case V4L2_PIX_FMT_QC08C:
607 return HFI_COLOR_FORMAT_NV12_UBWC;
608 case V4L2_PIX_FMT_QC10C:
609 return HFI_COLOR_FORMAT_YUV420_TP10_UBWC;
610 case V4L2_PIX_FMT_P010:
611 return HFI_COLOR_FORMAT_P010;
619 static int platform_get_bufreq(struct venus_inst *inst, u32 buftype,
620 struct hfi_buffer_requirements *req)
622 enum hfi_version version = inst->core->res->hfi_version;
623 const struct hfi_platform *hfi_plat;
624 struct hfi_plat_buffers_params params;
625 bool is_dec = inst->session_type == VIDC_SESSION_TYPE_DEC;
626 struct venc_controls *enc_ctr = &inst->controls.enc;
628 hfi_plat = hfi_platform_get(version);
630 if (!hfi_plat || !hfi_plat->bufreq)
633 params.version = version;
634 params.num_vpp_pipes = inst->core->res->num_vpp_pipes;
637 params.width = inst->width;
638 params.height = inst->height;
639 params.out_width = inst->out_width;
640 params.out_height = inst->out_height;
641 params.codec = inst->fmt_out->pixfmt;
642 params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_cap->pixfmt);
643 params.dec.max_mbs_per_frame = mbs_per_frame_max(inst);
644 params.dec.buffer_size_limit = 0;
645 params.dec.is_secondary_output =
646 inst->opb_buftype == HFI_BUFFER_OUTPUT2;
647 if (params.dec.is_secondary_output)
648 params.hfi_dpb_color_fmt = inst->dpb_fmt;
649 params.dec.is_interlaced =
650 inst->pic_struct != HFI_INTERLACE_FRAME_PROGRESSIVE;
652 params.width = inst->out_width;
653 params.height = inst->out_height;
654 params.codec = inst->fmt_cap->pixfmt;
655 params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_out->pixfmt);
656 params.enc.work_mode = VIDC_WORK_MODE_2;
657 params.enc.rc_type = HFI_RATE_CONTROL_OFF;
658 if (enc_ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
659 params.enc.rc_type = HFI_RATE_CONTROL_CQ;
660 params.enc.num_b_frames = enc_ctr->num_b_frames;
661 params.enc.is_tenbit = inst->bit_depth == VIDC_BITDEPTH_10;
664 return hfi_plat->bufreq(¶ms, inst->session_type, buftype, req);
667 int venus_helper_get_bufreq(struct venus_inst *inst, u32 type,
668 struct hfi_buffer_requirements *req)
670 u32 ptype = HFI_PROPERTY_CONFIG_BUFFER_REQUIREMENTS;
671 enum hfi_version ver = inst->core->res->hfi_version;
672 union hfi_get_property hprop;
676 memset(req, 0, sizeof(*req));
678 if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
679 hfi_bufreq_set_count_min(req, ver, inst->fw_min_cnt);
681 ret = platform_get_bufreq(inst, type, req);
683 if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
684 inst->fw_min_cnt = hfi_bufreq_get_count_min(req, ver);
688 ret = hfi_session_get_property(inst, ptype, &hprop);
694 for (i = 0; i < HFI_BUFFER_TYPE_MAX; i++) {
695 if (hprop.bufreq[i].type != type)
698 memcpy(req, &hprop.bufreq[i], sizeof(*req));
705 EXPORT_SYMBOL_GPL(venus_helper_get_bufreq);
712 static const struct id_mapping mpeg4_profiles[] = {
713 { HFI_MPEG4_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE },
714 { HFI_MPEG4_PROFILE_ADVANCEDSIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE },
717 static const struct id_mapping mpeg4_levels[] = {
718 { HFI_MPEG4_LEVEL_0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 },
719 { HFI_MPEG4_LEVEL_0b, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B },
720 { HFI_MPEG4_LEVEL_1, V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 },
721 { HFI_MPEG4_LEVEL_2, V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 },
722 { HFI_MPEG4_LEVEL_3, V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 },
723 { HFI_MPEG4_LEVEL_4, V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 },
724 { HFI_MPEG4_LEVEL_5, V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 },
727 static const struct id_mapping mpeg2_profiles[] = {
728 { HFI_MPEG2_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SIMPLE },
729 { HFI_MPEG2_PROFILE_MAIN, V4L2_MPEG_VIDEO_MPEG2_PROFILE_MAIN },
730 { HFI_MPEG2_PROFILE_SNR, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SNR_SCALABLE },
731 { HFI_MPEG2_PROFILE_SPATIAL, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SPATIALLY_SCALABLE },
732 { HFI_MPEG2_PROFILE_HIGH, V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH },
735 static const struct id_mapping mpeg2_levels[] = {
736 { HFI_MPEG2_LEVEL_LL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_LOW },
737 { HFI_MPEG2_LEVEL_ML, V4L2_MPEG_VIDEO_MPEG2_LEVEL_MAIN },
738 { HFI_MPEG2_LEVEL_H14, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH_1440 },
739 { HFI_MPEG2_LEVEL_HL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH },
742 static const struct id_mapping h264_profiles[] = {
743 { HFI_H264_PROFILE_BASELINE, V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE },
744 { HFI_H264_PROFILE_MAIN, V4L2_MPEG_VIDEO_H264_PROFILE_MAIN },
745 { HFI_H264_PROFILE_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH },
746 { HFI_H264_PROFILE_STEREO_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH },
747 { HFI_H264_PROFILE_MULTIVIEW_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH },
748 { HFI_H264_PROFILE_CONSTRAINED_BASE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE },
749 { HFI_H264_PROFILE_CONSTRAINED_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH },
752 static const struct id_mapping h264_levels[] = {
753 { HFI_H264_LEVEL_1, V4L2_MPEG_VIDEO_H264_LEVEL_1_0 },
754 { HFI_H264_LEVEL_1b, V4L2_MPEG_VIDEO_H264_LEVEL_1B },
755 { HFI_H264_LEVEL_11, V4L2_MPEG_VIDEO_H264_LEVEL_1_1 },
756 { HFI_H264_LEVEL_12, V4L2_MPEG_VIDEO_H264_LEVEL_1_2 },
757 { HFI_H264_LEVEL_13, V4L2_MPEG_VIDEO_H264_LEVEL_1_3 },
758 { HFI_H264_LEVEL_2, V4L2_MPEG_VIDEO_H264_LEVEL_2_0 },
759 { HFI_H264_LEVEL_21, V4L2_MPEG_VIDEO_H264_LEVEL_2_1 },
760 { HFI_H264_LEVEL_22, V4L2_MPEG_VIDEO_H264_LEVEL_2_2 },
761 { HFI_H264_LEVEL_3, V4L2_MPEG_VIDEO_H264_LEVEL_3_0 },
762 { HFI_H264_LEVEL_31, V4L2_MPEG_VIDEO_H264_LEVEL_3_1 },
763 { HFI_H264_LEVEL_32, V4L2_MPEG_VIDEO_H264_LEVEL_3_2 },
764 { HFI_H264_LEVEL_4, V4L2_MPEG_VIDEO_H264_LEVEL_4_0 },
765 { HFI_H264_LEVEL_41, V4L2_MPEG_VIDEO_H264_LEVEL_4_1 },
766 { HFI_H264_LEVEL_42, V4L2_MPEG_VIDEO_H264_LEVEL_4_2 },
767 { HFI_H264_LEVEL_5, V4L2_MPEG_VIDEO_H264_LEVEL_5_0 },
768 { HFI_H264_LEVEL_51, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
769 { HFI_H264_LEVEL_52, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
772 static const struct id_mapping hevc_profiles[] = {
773 { HFI_HEVC_PROFILE_MAIN, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN },
774 { HFI_HEVC_PROFILE_MAIN_STILL_PIC, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE },
775 { HFI_HEVC_PROFILE_MAIN10, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10 },
778 static const struct id_mapping hevc_levels[] = {
779 { HFI_HEVC_LEVEL_1, V4L2_MPEG_VIDEO_HEVC_LEVEL_1 },
780 { HFI_HEVC_LEVEL_2, V4L2_MPEG_VIDEO_HEVC_LEVEL_2 },
781 { HFI_HEVC_LEVEL_21, V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1 },
782 { HFI_HEVC_LEVEL_3, V4L2_MPEG_VIDEO_HEVC_LEVEL_3 },
783 { HFI_HEVC_LEVEL_31, V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1 },
784 { HFI_HEVC_LEVEL_4, V4L2_MPEG_VIDEO_HEVC_LEVEL_4 },
785 { HFI_HEVC_LEVEL_41, V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1 },
786 { HFI_HEVC_LEVEL_5, V4L2_MPEG_VIDEO_HEVC_LEVEL_5 },
787 { HFI_HEVC_LEVEL_51, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1 },
788 { HFI_HEVC_LEVEL_52, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2 },
789 { HFI_HEVC_LEVEL_6, V4L2_MPEG_VIDEO_HEVC_LEVEL_6 },
790 { HFI_HEVC_LEVEL_61, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1 },
791 { HFI_HEVC_LEVEL_62, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2 },
794 static const struct id_mapping vp8_profiles[] = {
795 { HFI_VPX_PROFILE_VERSION_0, V4L2_MPEG_VIDEO_VP8_PROFILE_0 },
796 { HFI_VPX_PROFILE_VERSION_1, V4L2_MPEG_VIDEO_VP8_PROFILE_1 },
797 { HFI_VPX_PROFILE_VERSION_2, V4L2_MPEG_VIDEO_VP8_PROFILE_2 },
798 { HFI_VPX_PROFILE_VERSION_3, V4L2_MPEG_VIDEO_VP8_PROFILE_3 },
801 static const struct id_mapping vp9_profiles[] = {
802 { HFI_VP9_PROFILE_P0, V4L2_MPEG_VIDEO_VP9_PROFILE_0 },
803 { HFI_VP9_PROFILE_P2_10B, V4L2_MPEG_VIDEO_VP9_PROFILE_2 },
806 static const struct id_mapping vp9_levels[] = {
807 { HFI_VP9_LEVEL_1, V4L2_MPEG_VIDEO_VP9_LEVEL_1_0 },
808 { HFI_VP9_LEVEL_11, V4L2_MPEG_VIDEO_VP9_LEVEL_1_1 },
809 { HFI_VP9_LEVEL_2, V4L2_MPEG_VIDEO_VP9_LEVEL_2_0},
810 { HFI_VP9_LEVEL_21, V4L2_MPEG_VIDEO_VP9_LEVEL_2_1 },
811 { HFI_VP9_LEVEL_3, V4L2_MPEG_VIDEO_VP9_LEVEL_3_0},
812 { HFI_VP9_LEVEL_31, V4L2_MPEG_VIDEO_VP9_LEVEL_3_1 },
813 { HFI_VP9_LEVEL_4, V4L2_MPEG_VIDEO_VP9_LEVEL_4_0 },
814 { HFI_VP9_LEVEL_41, V4L2_MPEG_VIDEO_VP9_LEVEL_4_1 },
815 { HFI_VP9_LEVEL_5, V4L2_MPEG_VIDEO_VP9_LEVEL_5_0 },
816 { HFI_VP9_LEVEL_51, V4L2_MPEG_VIDEO_VP9_LEVEL_5_1 },
817 { HFI_VP9_LEVEL_6, V4L2_MPEG_VIDEO_VP9_LEVEL_6_0 },
818 { HFI_VP9_LEVEL_61, V4L2_MPEG_VIDEO_VP9_LEVEL_6_1 },
821 static u32 find_v4l2_id(u32 hfi_id, const struct id_mapping *array, unsigned int array_sz)
825 if (!array || !array_sz)
828 for (i = 0; i < array_sz; i++)
829 if (hfi_id == array[i].hfi_id)
830 return array[i].v4l2_id;
835 static u32 find_hfi_id(u32 v4l2_id, const struct id_mapping *array, unsigned int array_sz)
839 if (!array || !array_sz)
842 for (i = 0; i < array_sz; i++)
843 if (v4l2_id == array[i].v4l2_id)
844 return array[i].hfi_id;
850 v4l2_id_profile_level(u32 hfi_codec, struct hfi_profile_level *pl, u32 *profile, u32 *level)
852 u32 hfi_pf = pl->profile;
853 u32 hfi_lvl = pl->level;
856 case HFI_VIDEO_CODEC_H264:
857 *profile = find_v4l2_id(hfi_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
858 *level = find_v4l2_id(hfi_lvl, h264_levels, ARRAY_SIZE(h264_levels));
860 case HFI_VIDEO_CODEC_MPEG2:
861 *profile = find_v4l2_id(hfi_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
862 *level = find_v4l2_id(hfi_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
864 case HFI_VIDEO_CODEC_MPEG4:
865 *profile = find_v4l2_id(hfi_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
866 *level = find_v4l2_id(hfi_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
868 case HFI_VIDEO_CODEC_VP8:
869 *profile = find_v4l2_id(hfi_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
872 case HFI_VIDEO_CODEC_VP9:
873 *profile = find_v4l2_id(hfi_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
874 *level = find_v4l2_id(hfi_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
876 case HFI_VIDEO_CODEC_HEVC:
877 *profile = find_v4l2_id(hfi_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
878 *level = find_v4l2_id(hfi_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
886 hfi_id_profile_level(u32 hfi_codec, u32 v4l2_pf, u32 v4l2_lvl, struct hfi_profile_level *pl)
889 case HFI_VIDEO_CODEC_H264:
890 pl->profile = find_hfi_id(v4l2_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
891 pl->level = find_hfi_id(v4l2_lvl, h264_levels, ARRAY_SIZE(h264_levels));
893 case HFI_VIDEO_CODEC_MPEG2:
894 pl->profile = find_hfi_id(v4l2_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
895 pl->level = find_hfi_id(v4l2_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
897 case HFI_VIDEO_CODEC_MPEG4:
898 pl->profile = find_hfi_id(v4l2_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
899 pl->level = find_hfi_id(v4l2_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
901 case HFI_VIDEO_CODEC_VP8:
902 pl->profile = find_hfi_id(v4l2_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
905 case HFI_VIDEO_CODEC_VP9:
906 pl->profile = find_hfi_id(v4l2_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
907 pl->level = find_hfi_id(v4l2_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
909 case HFI_VIDEO_CODEC_HEVC:
910 pl->profile = find_hfi_id(v4l2_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
911 pl->level = find_hfi_id(v4l2_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
918 int venus_helper_get_profile_level(struct venus_inst *inst, u32 *profile, u32 *level)
920 const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
921 union hfi_get_property hprop;
924 ret = hfi_session_get_property(inst, ptype, &hprop);
928 v4l2_id_profile_level(inst->hfi_codec, &hprop.profile_level, profile, level);
932 EXPORT_SYMBOL_GPL(venus_helper_get_profile_level);
934 int venus_helper_set_profile_level(struct venus_inst *inst, u32 profile, u32 level)
936 const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
937 struct hfi_profile_level pl;
939 hfi_id_profile_level(inst->hfi_codec, profile, level, &pl);
941 return hfi_session_set_property(inst, ptype, &pl);
943 EXPORT_SYMBOL_GPL(venus_helper_set_profile_level);
945 static u32 get_framesize_raw_nv12(u32 width, u32 height)
947 u32 y_stride, uv_stride, y_plane;
948 u32 y_sclines, uv_sclines, uv_plane;
951 y_stride = ALIGN(width, 128);
952 uv_stride = ALIGN(width, 128);
953 y_sclines = ALIGN(height, 32);
954 uv_sclines = ALIGN(((height + 1) >> 1), 16);
956 y_plane = y_stride * y_sclines;
957 uv_plane = uv_stride * uv_sclines + SZ_4K;
958 size = y_plane + uv_plane + SZ_8K;
960 return ALIGN(size, SZ_4K);
963 static u32 get_framesize_raw_nv12_ubwc(u32 width, u32 height)
965 u32 y_meta_stride, y_meta_plane;
966 u32 y_stride, y_plane;
967 u32 uv_meta_stride, uv_meta_plane;
968 u32 uv_stride, uv_plane;
969 u32 extradata = SZ_16K;
971 y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
972 y_meta_plane = y_meta_stride * ALIGN(DIV_ROUND_UP(height, 8), 16);
973 y_meta_plane = ALIGN(y_meta_plane, SZ_4K);
975 y_stride = ALIGN(width, 128);
976 y_plane = ALIGN(y_stride * ALIGN(height, 32), SZ_4K);
978 uv_meta_stride = ALIGN(DIV_ROUND_UP(width / 2, 16), 64);
979 uv_meta_plane = uv_meta_stride * ALIGN(DIV_ROUND_UP(height / 2, 8), 16);
980 uv_meta_plane = ALIGN(uv_meta_plane, SZ_4K);
982 uv_stride = ALIGN(width, 128);
983 uv_plane = ALIGN(uv_stride * ALIGN(height / 2, 32), SZ_4K);
985 return ALIGN(y_meta_plane + y_plane + uv_meta_plane + uv_plane +
986 max(extradata, y_stride * 48), SZ_4K);
989 static u32 get_framesize_raw_p010(u32 width, u32 height)
991 u32 y_plane, uv_plane, y_stride, uv_stride, y_sclines, uv_sclines;
993 y_stride = ALIGN(width * 2, 128);
994 uv_stride = ALIGN(width * 2, 128);
995 y_sclines = ALIGN(height, 32);
996 uv_sclines = ALIGN((height + 1) >> 1, 16);
997 y_plane = y_stride * y_sclines;
998 uv_plane = uv_stride * uv_sclines;
1000 return ALIGN((y_plane + uv_plane), SZ_4K);
1003 static u32 get_framesize_raw_p010_ubwc(u32 width, u32 height)
1005 u32 y_stride, uv_stride, y_sclines, uv_sclines;
1006 u32 y_ubwc_plane, uv_ubwc_plane;
1007 u32 y_meta_stride, y_meta_scanlines;
1008 u32 uv_meta_stride, uv_meta_scanlines;
1009 u32 y_meta_plane, uv_meta_plane;
1012 y_stride = ALIGN(width * 2, 256);
1013 uv_stride = ALIGN(width * 2, 256);
1014 y_sclines = ALIGN(height, 16);
1015 uv_sclines = ALIGN((height + 1) >> 1, 16);
1017 y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
1018 uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
1019 y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
1020 y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
1021 y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
1022 uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 16), 64);
1023 uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
1024 uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
1026 size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
1028 return ALIGN(size, SZ_4K);
1031 static u32 get_framesize_raw_yuv420_tp10_ubwc(u32 width, u32 height)
1033 u32 y_stride, uv_stride, y_sclines, uv_sclines;
1034 u32 y_ubwc_plane, uv_ubwc_plane;
1035 u32 y_meta_stride, y_meta_scanlines;
1036 u32 uv_meta_stride, uv_meta_scanlines;
1037 u32 y_meta_plane, uv_meta_plane;
1038 u32 extradata = SZ_16K;
1041 y_stride = ALIGN(width * 4 / 3, 256);
1042 uv_stride = ALIGN(width * 4 / 3, 256);
1043 y_sclines = ALIGN(height, 16);
1044 uv_sclines = ALIGN((height + 1) >> 1, 16);
1046 y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
1047 uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
1048 y_meta_stride = ALIGN(DIV_ROUND_UP(width, 48), 64);
1049 y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
1050 y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
1051 uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 24), 64);
1052 uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
1053 uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
1055 size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
1056 size += max(extradata + SZ_8K, y_stride * 48);
1058 return ALIGN(size, SZ_4K);
1061 u32 venus_helper_get_framesz_raw(u32 hfi_fmt, u32 width, u32 height)
1064 case HFI_COLOR_FORMAT_NV12:
1065 case HFI_COLOR_FORMAT_NV21:
1066 return get_framesize_raw_nv12(width, height);
1067 case HFI_COLOR_FORMAT_NV12_UBWC:
1068 return get_framesize_raw_nv12_ubwc(width, height);
1069 case HFI_COLOR_FORMAT_P010:
1070 return get_framesize_raw_p010(width, height);
1071 case HFI_COLOR_FORMAT_P010_UBWC:
1072 return get_framesize_raw_p010_ubwc(width, height);
1073 case HFI_COLOR_FORMAT_YUV420_TP10_UBWC:
1074 return get_framesize_raw_yuv420_tp10_ubwc(width, height);
1079 EXPORT_SYMBOL_GPL(venus_helper_get_framesz_raw);
1081 u32 venus_helper_get_framesz(u32 v4l2_fmt, u32 width, u32 height)
1087 case V4L2_PIX_FMT_MPEG:
1088 case V4L2_PIX_FMT_H264:
1089 case V4L2_PIX_FMT_H264_NO_SC:
1090 case V4L2_PIX_FMT_H264_MVC:
1091 case V4L2_PIX_FMT_H263:
1092 case V4L2_PIX_FMT_MPEG1:
1093 case V4L2_PIX_FMT_MPEG2:
1094 case V4L2_PIX_FMT_MPEG4:
1095 case V4L2_PIX_FMT_XVID:
1096 case V4L2_PIX_FMT_VC1_ANNEX_G:
1097 case V4L2_PIX_FMT_VC1_ANNEX_L:
1098 case V4L2_PIX_FMT_VP8:
1099 case V4L2_PIX_FMT_VP9:
1100 case V4L2_PIX_FMT_HEVC:
1109 sz = ALIGN(height, 32) * ALIGN(width, 32) * 3 / 2 / 2;
1110 if (width < 1280 || height < 720)
1112 return ALIGN(sz, SZ_4K);
1115 hfi_fmt = to_hfi_raw_fmt(v4l2_fmt);
1119 return venus_helper_get_framesz_raw(hfi_fmt, width, height);
1121 EXPORT_SYMBOL_GPL(venus_helper_get_framesz);
1123 int venus_helper_set_input_resolution(struct venus_inst *inst,
1124 unsigned int width, unsigned int height)
1126 u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
1127 struct hfi_framesize fs;
1129 fs.buffer_type = HFI_BUFFER_INPUT;
1133 return hfi_session_set_property(inst, ptype, &fs);
1135 EXPORT_SYMBOL_GPL(venus_helper_set_input_resolution);
1137 int venus_helper_set_output_resolution(struct venus_inst *inst,
1138 unsigned int width, unsigned int height,
1141 u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
1142 struct hfi_framesize fs;
1144 fs.buffer_type = buftype;
1148 return hfi_session_set_property(inst, ptype, &fs);
1150 EXPORT_SYMBOL_GPL(venus_helper_set_output_resolution);
1152 static u32 venus_helper_get_work_mode(struct venus_inst *inst)
1157 mode = VIDC_WORK_MODE_2;
1158 if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
1159 num_mbs = (ALIGN(inst->height, 16) * ALIGN(inst->width, 16)) / 256;
1160 if (inst->hfi_codec == HFI_VIDEO_CODEC_MPEG2 ||
1161 inst->pic_struct != HFI_INTERLACE_FRAME_PROGRESSIVE ||
1162 num_mbs <= NUM_MBS_720P)
1163 mode = VIDC_WORK_MODE_1;
1165 num_mbs = (ALIGN(inst->out_height, 16) * ALIGN(inst->out_width, 16)) / 256;
1166 if (inst->hfi_codec == HFI_VIDEO_CODEC_VP8 &&
1167 num_mbs <= NUM_MBS_4K)
1168 mode = VIDC_WORK_MODE_1;
1174 int venus_helper_set_work_mode(struct venus_inst *inst)
1176 const u32 ptype = HFI_PROPERTY_PARAM_WORK_MODE;
1177 struct hfi_video_work_mode wm;
1180 if (!IS_V4(inst->core) && !IS_V6(inst->core))
1183 mode = venus_helper_get_work_mode(inst);
1184 wm.video_work_mode = mode;
1185 return hfi_session_set_property(inst, ptype, &wm);
1187 EXPORT_SYMBOL_GPL(venus_helper_set_work_mode);
1189 int venus_helper_set_format_constraints(struct venus_inst *inst)
1191 const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_PLANE_ACTUAL_CONSTRAINTS_INFO;
1192 struct hfi_uncompressed_plane_actual_constraints_info pconstraint;
1194 if (!IS_V6(inst->core))
1197 if (inst->opb_fmt == HFI_COLOR_FORMAT_NV12_UBWC ||
1198 inst->opb_fmt == HFI_COLOR_FORMAT_YUV420_TP10_UBWC)
1201 pconstraint.buffer_type = HFI_BUFFER_OUTPUT2;
1202 pconstraint.num_planes = 2;
1203 pconstraint.plane_format[0].stride_multiples = 128;
1204 pconstraint.plane_format[0].max_stride = 8192;
1205 pconstraint.plane_format[0].min_plane_buffer_height_multiple = 32;
1206 pconstraint.plane_format[0].buffer_alignment = 256;
1208 pconstraint.plane_format[1].stride_multiples = 128;
1209 pconstraint.plane_format[1].max_stride = 8192;
1210 pconstraint.plane_format[1].min_plane_buffer_height_multiple = 16;
1211 pconstraint.plane_format[1].buffer_alignment = 256;
1213 return hfi_session_set_property(inst, ptype, &pconstraint);
1215 EXPORT_SYMBOL_GPL(venus_helper_set_format_constraints);
1217 int venus_helper_set_num_bufs(struct venus_inst *inst, unsigned int input_bufs,
1218 unsigned int output_bufs,
1219 unsigned int output2_bufs)
1221 u32 ptype = HFI_PROPERTY_PARAM_BUFFER_COUNT_ACTUAL;
1222 struct hfi_buffer_count_actual buf_count;
1225 buf_count.type = HFI_BUFFER_INPUT;
1226 buf_count.count_actual = input_bufs;
1228 ret = hfi_session_set_property(inst, ptype, &buf_count);
1232 buf_count.type = HFI_BUFFER_OUTPUT;
1233 buf_count.count_actual = output_bufs;
1235 ret = hfi_session_set_property(inst, ptype, &buf_count);
1240 buf_count.type = HFI_BUFFER_OUTPUT2;
1241 buf_count.count_actual = output2_bufs;
1243 ret = hfi_session_set_property(inst, ptype, &buf_count);
1248 EXPORT_SYMBOL_GPL(venus_helper_set_num_bufs);
1250 int venus_helper_set_raw_format(struct venus_inst *inst, u32 hfi_format,
1253 const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_FORMAT_SELECT;
1254 struct hfi_uncompressed_format_select fmt;
1256 fmt.buffer_type = buftype;
1257 fmt.format = hfi_format;
1259 return hfi_session_set_property(inst, ptype, &fmt);
1261 EXPORT_SYMBOL_GPL(venus_helper_set_raw_format);
1263 int venus_helper_set_color_format(struct venus_inst *inst, u32 pixfmt)
1265 u32 hfi_format, buftype;
1267 if (inst->session_type == VIDC_SESSION_TYPE_DEC)
1268 buftype = HFI_BUFFER_OUTPUT;
1269 else if (inst->session_type == VIDC_SESSION_TYPE_ENC)
1270 buftype = HFI_BUFFER_INPUT;
1274 hfi_format = to_hfi_raw_fmt(pixfmt);
1278 return venus_helper_set_raw_format(inst, hfi_format, buftype);
1280 EXPORT_SYMBOL_GPL(venus_helper_set_color_format);
1282 int venus_helper_set_multistream(struct venus_inst *inst, bool out_en,
1285 struct hfi_multi_stream multi = {0};
1286 u32 ptype = HFI_PROPERTY_PARAM_VDEC_MULTI_STREAM;
1289 multi.buffer_type = HFI_BUFFER_OUTPUT;
1290 multi.enable = out_en;
1292 ret = hfi_session_set_property(inst, ptype, &multi);
1296 multi.buffer_type = HFI_BUFFER_OUTPUT2;
1297 multi.enable = out2_en;
1299 return hfi_session_set_property(inst, ptype, &multi);
1301 EXPORT_SYMBOL_GPL(venus_helper_set_multistream);
1303 int venus_helper_set_dyn_bufmode(struct venus_inst *inst)
1305 const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_ALLOC_MODE;
1306 struct hfi_buffer_alloc_mode mode;
1309 if (!is_dynamic_bufmode(inst))
1312 mode.type = HFI_BUFFER_OUTPUT;
1313 mode.mode = HFI_BUFFER_MODE_DYNAMIC;
1315 ret = hfi_session_set_property(inst, ptype, &mode);
1319 mode.type = HFI_BUFFER_OUTPUT2;
1321 return hfi_session_set_property(inst, ptype, &mode);
1323 EXPORT_SYMBOL_GPL(venus_helper_set_dyn_bufmode);
1325 int venus_helper_set_bufsize(struct venus_inst *inst, u32 bufsize, u32 buftype)
1327 const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_SIZE_ACTUAL;
1328 struct hfi_buffer_size_actual bufsz;
1330 bufsz.type = buftype;
1331 bufsz.size = bufsize;
1333 return hfi_session_set_property(inst, ptype, &bufsz);
1335 EXPORT_SYMBOL_GPL(venus_helper_set_bufsize);
1337 unsigned int venus_helper_get_opb_size(struct venus_inst *inst)
1339 /* the encoder has only one output */
1340 if (inst->session_type == VIDC_SESSION_TYPE_ENC)
1341 return inst->output_buf_size;
1343 if (inst->opb_buftype == HFI_BUFFER_OUTPUT)
1344 return inst->output_buf_size;
1345 else if (inst->opb_buftype == HFI_BUFFER_OUTPUT2)
1346 return inst->output2_buf_size;
1350 EXPORT_SYMBOL_GPL(venus_helper_get_opb_size);
1352 static void delayed_process_buf_func(struct work_struct *work)
1354 struct venus_buffer *buf, *n;
1355 struct venus_inst *inst;
1358 inst = container_of(work, struct venus_inst, delayed_process_work);
1360 mutex_lock(&inst->lock);
1362 if (!(inst->streamon_out & inst->streamon_cap))
1365 list_for_each_entry_safe(buf, n, &inst->delayed_process, ref_list) {
1366 if (buf->flags & HFI_BUFFERFLAG_READONLY)
1369 ret = session_process_buf(inst, &buf->vb);
1371 return_buf_error(inst, &buf->vb);
1373 list_del_init(&buf->ref_list);
1376 mutex_unlock(&inst->lock);
1379 void venus_helper_release_buf_ref(struct venus_inst *inst, unsigned int idx)
1381 struct venus_buffer *buf;
1383 list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
1384 if (buf->vb.vb2_buf.index == idx) {
1385 buf->flags &= ~HFI_BUFFERFLAG_READONLY;
1386 schedule_work(&inst->delayed_process_work);
1391 EXPORT_SYMBOL_GPL(venus_helper_release_buf_ref);
1393 void venus_helper_acquire_buf_ref(struct vb2_v4l2_buffer *vbuf)
1395 struct venus_buffer *buf = to_venus_buffer(vbuf);
1397 buf->flags |= HFI_BUFFERFLAG_READONLY;
1399 EXPORT_SYMBOL_GPL(venus_helper_acquire_buf_ref);
1401 static int is_buf_refed(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
1403 struct venus_buffer *buf = to_venus_buffer(vbuf);
1405 if (buf->flags & HFI_BUFFERFLAG_READONLY) {
1406 list_add_tail(&buf->ref_list, &inst->delayed_process);
1407 schedule_work(&inst->delayed_process_work);
1414 struct vb2_v4l2_buffer *
1415 venus_helper_find_buf(struct venus_inst *inst, unsigned int type, u32 idx)
1417 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1419 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1420 return v4l2_m2m_src_buf_remove_by_idx(m2m_ctx, idx);
1422 return v4l2_m2m_dst_buf_remove_by_idx(m2m_ctx, idx);
1424 EXPORT_SYMBOL_GPL(venus_helper_find_buf);
1426 void venus_helper_change_dpb_owner(struct venus_inst *inst,
1427 struct vb2_v4l2_buffer *vbuf, unsigned int type,
1428 unsigned int buf_type, u32 tag)
1430 struct intbuf *dpb_buf;
1432 if (!V4L2_TYPE_IS_CAPTURE(type) ||
1433 buf_type != inst->dpb_buftype)
1436 list_for_each_entry(dpb_buf, &inst->dpbbufs, list)
1437 if (dpb_buf->dpb_out_tag == tag) {
1438 dpb_buf->owned_by = DRIVER;
1442 EXPORT_SYMBOL_GPL(venus_helper_change_dpb_owner);
1444 int venus_helper_vb2_buf_init(struct vb2_buffer *vb)
1446 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1447 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1448 struct venus_buffer *buf = to_venus_buffer(vbuf);
1450 buf->size = vb2_plane_size(vb, 0);
1451 buf->dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1453 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1454 list_add_tail(&buf->reg_list, &inst->registeredbufs);
1458 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_init);
1460 int venus_helper_vb2_buf_prepare(struct vb2_buffer *vb)
1462 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1463 unsigned int out_buf_size = venus_helper_get_opb_size(inst);
1464 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1466 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1467 if (vbuf->field == V4L2_FIELD_ANY)
1468 vbuf->field = V4L2_FIELD_NONE;
1469 if (vbuf->field != V4L2_FIELD_NONE) {
1470 dev_err(inst->core->dev, "%s field isn't supported\n",
1476 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
1477 vb2_plane_size(vb, 0) < out_buf_size)
1479 if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1480 vb2_plane_size(vb, 0) < inst->input_buf_size)
1485 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_prepare);
1487 static void cache_payload(struct venus_inst *inst, struct vb2_buffer *vb)
1489 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1490 unsigned int idx = vbuf->vb2_buf.index;
1492 if (vbuf->vb2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1493 inst->payloads[idx] = vb2_get_plane_payload(vb, 0);
1496 void venus_helper_vb2_buf_queue(struct vb2_buffer *vb)
1498 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1499 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1500 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1503 v4l2_m2m_buf_queue(m2m_ctx, vbuf);
1505 /* Skip processing queued capture buffers after LAST flag */
1506 if (inst->session_type == VIDC_SESSION_TYPE_DEC &&
1507 V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1508 inst->codec_state == VENUS_DEC_STATE_DRC)
1511 cache_payload(inst, vb);
1513 if (inst->session_type == VIDC_SESSION_TYPE_ENC &&
1514 !(inst->streamon_out && inst->streamon_cap))
1517 if (vb2_start_streaming_called(vb->vb2_queue)) {
1518 ret = is_buf_refed(inst, vbuf);
1522 ret = session_process_buf(inst, vbuf);
1524 return_buf_error(inst, vbuf);
1527 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_queue);
1529 void venus_helper_buffers_done(struct venus_inst *inst, unsigned int type,
1530 enum vb2_buffer_state state)
1532 struct vb2_v4l2_buffer *buf;
1534 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1535 while ((buf = v4l2_m2m_src_buf_remove(inst->m2m_ctx)))
1536 v4l2_m2m_buf_done(buf, state);
1537 } else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1538 while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1539 v4l2_m2m_buf_done(buf, state);
1542 EXPORT_SYMBOL_GPL(venus_helper_buffers_done);
1544 void venus_helper_vb2_stop_streaming(struct vb2_queue *q)
1546 struct venus_inst *inst = vb2_get_drv_priv(q);
1547 struct venus_core *core = inst->core;
1550 mutex_lock(&inst->lock);
1552 if (inst->streamon_out & inst->streamon_cap) {
1553 ret = hfi_session_stop(inst);
1554 ret |= hfi_session_unload_res(inst);
1555 ret |= venus_helper_unregister_bufs(inst);
1556 ret |= venus_helper_intbufs_free(inst);
1557 ret |= hfi_session_deinit(inst);
1559 if (inst->session_error || test_bit(0, &core->sys_error))
1563 hfi_session_abort(inst);
1565 venus_helper_free_dpb_bufs(inst);
1567 venus_pm_load_scale(inst);
1568 INIT_LIST_HEAD(&inst->registeredbufs);
1571 venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
1572 VB2_BUF_STATE_ERROR);
1573 venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1574 VB2_BUF_STATE_ERROR);
1576 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1577 inst->streamon_out = 0;
1579 inst->streamon_cap = 0;
1581 venus_pm_release_core(inst);
1583 inst->session_error = 0;
1585 mutex_unlock(&inst->lock);
1587 EXPORT_SYMBOL_GPL(venus_helper_vb2_stop_streaming);
1589 void venus_helper_vb2_queue_error(struct venus_inst *inst)
1591 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1592 struct vb2_queue *q;
1594 q = v4l2_m2m_get_src_vq(m2m_ctx);
1596 q = v4l2_m2m_get_dst_vq(m2m_ctx);
1599 EXPORT_SYMBOL_GPL(venus_helper_vb2_queue_error);
1601 int venus_helper_process_initial_cap_bufs(struct venus_inst *inst)
1603 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1604 struct v4l2_m2m_buffer *buf, *n;
1607 v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
1608 ret = session_process_buf(inst, &buf->vb);
1610 return_buf_error(inst, &buf->vb);
1617 EXPORT_SYMBOL_GPL(venus_helper_process_initial_cap_bufs);
1619 int venus_helper_process_initial_out_bufs(struct venus_inst *inst)
1621 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1622 struct v4l2_m2m_buffer *buf, *n;
1625 v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
1626 ret = session_process_buf(inst, &buf->vb);
1628 return_buf_error(inst, &buf->vb);
1635 EXPORT_SYMBOL_GPL(venus_helper_process_initial_out_bufs);
1637 int venus_helper_vb2_start_streaming(struct venus_inst *inst)
1641 ret = venus_helper_intbufs_alloc(inst);
1645 ret = session_register_bufs(inst);
1649 venus_pm_load_scale(inst);
1651 ret = hfi_session_load_res(inst);
1653 goto err_unreg_bufs;
1655 ret = hfi_session_start(inst);
1657 goto err_unload_res;
1662 hfi_session_unload_res(inst);
1664 venus_helper_unregister_bufs(inst);
1666 venus_helper_intbufs_free(inst);
1669 EXPORT_SYMBOL_GPL(venus_helper_vb2_start_streaming);
1671 void venus_helper_m2m_device_run(void *priv)
1673 struct venus_inst *inst = priv;
1674 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1675 struct v4l2_m2m_buffer *buf, *n;
1678 mutex_lock(&inst->lock);
1680 v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
1681 ret = session_process_buf(inst, &buf->vb);
1683 return_buf_error(inst, &buf->vb);
1686 v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
1687 ret = session_process_buf(inst, &buf->vb);
1689 return_buf_error(inst, &buf->vb);
1692 mutex_unlock(&inst->lock);
1694 EXPORT_SYMBOL_GPL(venus_helper_m2m_device_run);
1696 void venus_helper_m2m_job_abort(void *priv)
1698 struct venus_inst *inst = priv;
1700 v4l2_m2m_job_finish(inst->m2m_dev, inst->m2m_ctx);
1702 EXPORT_SYMBOL_GPL(venus_helper_m2m_job_abort);
1704 int venus_helper_session_init(struct venus_inst *inst)
1706 enum hfi_version version = inst->core->res->hfi_version;
1707 u32 session_type = inst->session_type;
1711 codec = inst->session_type == VIDC_SESSION_TYPE_DEC ?
1712 inst->fmt_out->pixfmt : inst->fmt_cap->pixfmt;
1714 ret = hfi_session_init(inst, codec);
1718 inst->clk_data.vpp_freq = hfi_platform_get_codec_vpp_freq(version, codec,
1720 inst->clk_data.vsp_freq = hfi_platform_get_codec_vsp_freq(version, codec,
1722 inst->clk_data.low_power_freq = hfi_platform_get_codec_lp_freq(version, codec,
1727 EXPORT_SYMBOL_GPL(venus_helper_session_init);
1729 void venus_helper_init_instance(struct venus_inst *inst)
1731 if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
1732 INIT_LIST_HEAD(&inst->delayed_process);
1733 INIT_WORK(&inst->delayed_process_work,
1734 delayed_process_buf_func);
1737 EXPORT_SYMBOL_GPL(venus_helper_init_instance);
1739 static bool find_fmt_from_caps(struct hfi_plat_caps *caps, u32 buftype, u32 fmt)
1743 for (i = 0; i < caps->num_fmts; i++) {
1744 if (caps->fmts[i].buftype == buftype &&
1745 caps->fmts[i].fmt == fmt)
1752 int venus_helper_get_out_fmts(struct venus_inst *inst, u32 v4l2_fmt,
1753 u32 *out_fmt, u32 *out2_fmt, bool ubwc)
1755 struct venus_core *core = inst->core;
1756 struct hfi_plat_caps *caps;
1757 u32 ubwc_fmt, fmt = to_hfi_raw_fmt(v4l2_fmt);
1758 bool found, found_ubwc;
1760 *out_fmt = *out2_fmt = 0;
1765 caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
1769 if (inst->bit_depth == VIDC_BITDEPTH_10 && inst->session_type == VIDC_SESSION_TYPE_DEC) {
1770 found_ubwc = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT,
1771 HFI_COLOR_FORMAT_YUV420_TP10_UBWC);
1772 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1773 if (found_ubwc && found) {
1775 * Hard-code DPB buffers to be 10bit UBWC
1776 * until V4L2 is able to expose compressed/tiled
1777 * formats to applications.
1779 *out_fmt = HFI_COLOR_FORMAT_YUV420_TP10_UBWC;
1786 ubwc_fmt = fmt | HFI_COLOR_FORMAT_UBWC_BASE;
1787 found_ubwc = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT,
1789 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1791 if (found_ubwc && found) {
1792 *out_fmt = ubwc_fmt;
1798 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT, fmt);
1805 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1814 EXPORT_SYMBOL_GPL(venus_helper_get_out_fmts);
1816 bool venus_helper_check_format(struct venus_inst *inst, u32 v4l2_pixfmt)
1818 struct venus_core *core = inst->core;
1819 u32 fmt = to_hfi_raw_fmt(v4l2_pixfmt);
1820 struct hfi_plat_caps *caps;
1826 caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
1830 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT, fmt);
1834 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1838 EXPORT_SYMBOL_GPL(venus_helper_check_format);
1840 int venus_helper_set_stride(struct venus_inst *inst,
1841 unsigned int width, unsigned int height)
1843 const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_PLANE_ACTUAL_INFO;
1845 struct hfi_uncompressed_plane_actual_info plane_actual_info;
1847 plane_actual_info.buffer_type = HFI_BUFFER_INPUT;
1848 plane_actual_info.num_planes = 2;
1849 plane_actual_info.plane_format[0].actual_stride = width;
1850 plane_actual_info.plane_format[0].actual_plane_buffer_height = height;
1851 plane_actual_info.plane_format[1].actual_stride = width;
1852 plane_actual_info.plane_format[1].actual_plane_buffer_height = height / 2;
1854 return hfi_session_set_property(inst, ptype, &plane_actual_info);
1856 EXPORT_SYMBOL_GPL(venus_helper_set_stride);