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_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;
507 memset(&fdata, 0, sizeof(fdata));
508 fdata.alloc_len = buf->size;
509 fdata.device_addr = buf->dma_addr;
510 fdata.timestamp = vb->timestamp;
511 do_div(fdata.timestamp, NSEC_PER_USEC);
513 fdata.clnt_data = vbuf->vb2_buf.index;
515 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
516 fdata.buffer_type = HFI_BUFFER_INPUT;
517 fdata.filled_len = vb2_get_plane_payload(vb, 0);
518 fdata.offset = vb->planes[0].data_offset;
520 if (vbuf->flags & V4L2_BUF_FLAG_LAST || !fdata.filled_len)
521 fdata.flags |= HFI_BUFFERFLAG_EOS;
523 if (inst->session_type == VIDC_SESSION_TYPE_DEC)
524 put_ts_metadata(inst, vbuf);
526 venus_pm_load_scale(inst);
527 } else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
528 if (inst->session_type == VIDC_SESSION_TYPE_ENC)
529 fdata.buffer_type = HFI_BUFFER_OUTPUT;
531 fdata.buffer_type = inst->opb_buftype;
532 fdata.filled_len = 0;
536 ret = hfi_session_process_buf(inst, &fdata);
543 static bool is_dynamic_bufmode(struct venus_inst *inst)
545 struct venus_core *core = inst->core;
546 struct hfi_plat_caps *caps;
549 * v4 doesn't send BUFFER_ALLOC_MODE_SUPPORTED property and supports
550 * dynamic buffer mode by default for HFI_BUFFER_OUTPUT/OUTPUT2.
552 if (IS_V4(core) || IS_V6(core))
555 caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
559 return caps->cap_bufs_mode_dynamic;
562 int venus_helper_unregister_bufs(struct venus_inst *inst)
564 struct venus_buffer *buf, *n;
565 struct hfi_buffer_desc bd;
568 if (is_dynamic_bufmode(inst))
571 list_for_each_entry_safe(buf, n, &inst->registeredbufs, reg_list) {
572 fill_buffer_desc(buf, &bd, true);
573 ret = hfi_session_unset_buffers(inst, &bd);
574 list_del_init(&buf->reg_list);
579 EXPORT_SYMBOL_GPL(venus_helper_unregister_bufs);
581 static int session_register_bufs(struct venus_inst *inst)
583 struct venus_core *core = inst->core;
584 struct device *dev = core->dev;
585 struct hfi_buffer_desc bd;
586 struct venus_buffer *buf;
589 if (is_dynamic_bufmode(inst))
592 list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
593 fill_buffer_desc(buf, &bd, false);
594 ret = hfi_session_set_buffers(inst, &bd);
596 dev_err(dev, "%s: set buffer failed\n", __func__);
604 static u32 to_hfi_raw_fmt(u32 v4l2_fmt)
607 case V4L2_PIX_FMT_NV12:
608 return HFI_COLOR_FORMAT_NV12;
609 case V4L2_PIX_FMT_NV21:
610 return HFI_COLOR_FORMAT_NV21;
611 case V4L2_PIX_FMT_QC08C:
612 return HFI_COLOR_FORMAT_NV12_UBWC;
613 case V4L2_PIX_FMT_QC10C:
614 return HFI_COLOR_FORMAT_YUV420_TP10_UBWC;
622 static int platform_get_bufreq(struct venus_inst *inst, u32 buftype,
623 struct hfi_buffer_requirements *req)
625 enum hfi_version version = inst->core->res->hfi_version;
626 const struct hfi_platform *hfi_plat;
627 struct hfi_plat_buffers_params params;
628 bool is_dec = inst->session_type == VIDC_SESSION_TYPE_DEC;
629 struct venc_controls *enc_ctr = &inst->controls.enc;
631 hfi_plat = hfi_platform_get(version);
633 if (!hfi_plat || !hfi_plat->bufreq)
636 params.version = version;
637 params.num_vpp_pipes = inst->core->res->num_vpp_pipes;
640 params.width = inst->width;
641 params.height = inst->height;
642 params.codec = inst->fmt_out->pixfmt;
643 params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_cap->pixfmt);
644 params.dec.max_mbs_per_frame = mbs_per_frame_max(inst);
645 params.dec.buffer_size_limit = 0;
646 params.dec.is_secondary_output =
647 inst->opb_buftype == HFI_BUFFER_OUTPUT2;
648 params.dec.is_interlaced =
649 inst->pic_struct != HFI_INTERLACE_FRAME_PROGRESSIVE;
651 params.width = inst->out_width;
652 params.height = inst->out_height;
653 params.codec = inst->fmt_cap->pixfmt;
654 params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_out->pixfmt);
655 params.enc.work_mode = VIDC_WORK_MODE_2;
656 params.enc.rc_type = HFI_RATE_CONTROL_OFF;
657 if (enc_ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
658 params.enc.rc_type = HFI_RATE_CONTROL_CQ;
659 params.enc.num_b_frames = enc_ctr->num_b_frames;
660 params.enc.is_tenbit = inst->bit_depth == VIDC_BITDEPTH_10;
663 return hfi_plat->bufreq(¶ms, inst->session_type, buftype, req);
666 int venus_helper_get_bufreq(struct venus_inst *inst, u32 type,
667 struct hfi_buffer_requirements *req)
669 u32 ptype = HFI_PROPERTY_CONFIG_BUFFER_REQUIREMENTS;
670 union hfi_get_property hprop;
674 memset(req, 0, sizeof(*req));
676 if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
677 req->count_min = inst->fw_min_cnt;
679 ret = platform_get_bufreq(inst, type, req);
681 if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
682 inst->fw_min_cnt = req->count_min;
686 ret = hfi_session_get_property(inst, ptype, &hprop);
692 for (i = 0; i < HFI_BUFFER_TYPE_MAX; i++) {
693 if (hprop.bufreq[i].type != type)
696 memcpy(req, &hprop.bufreq[i], sizeof(*req));
703 EXPORT_SYMBOL_GPL(venus_helper_get_bufreq);
710 static const struct id_mapping mpeg4_profiles[] = {
711 { HFI_MPEG4_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE },
712 { HFI_MPEG4_PROFILE_ADVANCEDSIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE },
715 static const struct id_mapping mpeg4_levels[] = {
716 { HFI_MPEG4_LEVEL_0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 },
717 { HFI_MPEG4_LEVEL_0b, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B },
718 { HFI_MPEG4_LEVEL_1, V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 },
719 { HFI_MPEG4_LEVEL_2, V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 },
720 { HFI_MPEG4_LEVEL_3, V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 },
721 { HFI_MPEG4_LEVEL_4, V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 },
722 { HFI_MPEG4_LEVEL_5, V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 },
725 static const struct id_mapping mpeg2_profiles[] = {
726 { HFI_MPEG2_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SIMPLE },
727 { HFI_MPEG2_PROFILE_MAIN, V4L2_MPEG_VIDEO_MPEG2_PROFILE_MAIN },
728 { HFI_MPEG2_PROFILE_SNR, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SNR_SCALABLE },
729 { HFI_MPEG2_PROFILE_SPATIAL, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SPATIALLY_SCALABLE },
730 { HFI_MPEG2_PROFILE_HIGH, V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH },
733 static const struct id_mapping mpeg2_levels[] = {
734 { HFI_MPEG2_LEVEL_LL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_LOW },
735 { HFI_MPEG2_LEVEL_ML, V4L2_MPEG_VIDEO_MPEG2_LEVEL_MAIN },
736 { HFI_MPEG2_LEVEL_H14, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH_1440 },
737 { HFI_MPEG2_LEVEL_HL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH },
740 static const struct id_mapping h264_profiles[] = {
741 { HFI_H264_PROFILE_BASELINE, V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE },
742 { HFI_H264_PROFILE_MAIN, V4L2_MPEG_VIDEO_H264_PROFILE_MAIN },
743 { HFI_H264_PROFILE_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH },
744 { HFI_H264_PROFILE_STEREO_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH },
745 { HFI_H264_PROFILE_MULTIVIEW_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH },
746 { HFI_H264_PROFILE_CONSTRAINED_BASE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE },
747 { HFI_H264_PROFILE_CONSTRAINED_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH },
750 static const struct id_mapping h264_levels[] = {
751 { HFI_H264_LEVEL_1, V4L2_MPEG_VIDEO_H264_LEVEL_1_0 },
752 { HFI_H264_LEVEL_1b, V4L2_MPEG_VIDEO_H264_LEVEL_1B },
753 { HFI_H264_LEVEL_11, V4L2_MPEG_VIDEO_H264_LEVEL_1_1 },
754 { HFI_H264_LEVEL_12, V4L2_MPEG_VIDEO_H264_LEVEL_1_2 },
755 { HFI_H264_LEVEL_13, V4L2_MPEG_VIDEO_H264_LEVEL_1_3 },
756 { HFI_H264_LEVEL_2, V4L2_MPEG_VIDEO_H264_LEVEL_2_0 },
757 { HFI_H264_LEVEL_21, V4L2_MPEG_VIDEO_H264_LEVEL_2_1 },
758 { HFI_H264_LEVEL_22, V4L2_MPEG_VIDEO_H264_LEVEL_2_2 },
759 { HFI_H264_LEVEL_3, V4L2_MPEG_VIDEO_H264_LEVEL_3_0 },
760 { HFI_H264_LEVEL_31, V4L2_MPEG_VIDEO_H264_LEVEL_3_1 },
761 { HFI_H264_LEVEL_32, V4L2_MPEG_VIDEO_H264_LEVEL_3_2 },
762 { HFI_H264_LEVEL_4, V4L2_MPEG_VIDEO_H264_LEVEL_4_0 },
763 { HFI_H264_LEVEL_41, V4L2_MPEG_VIDEO_H264_LEVEL_4_1 },
764 { HFI_H264_LEVEL_42, V4L2_MPEG_VIDEO_H264_LEVEL_4_2 },
765 { HFI_H264_LEVEL_5, V4L2_MPEG_VIDEO_H264_LEVEL_5_0 },
766 { HFI_H264_LEVEL_51, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
767 { HFI_H264_LEVEL_52, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
770 static const struct id_mapping hevc_profiles[] = {
771 { HFI_HEVC_PROFILE_MAIN, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN },
772 { HFI_HEVC_PROFILE_MAIN_STILL_PIC, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE },
773 { HFI_HEVC_PROFILE_MAIN10, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10 },
776 static const struct id_mapping hevc_levels[] = {
777 { HFI_HEVC_LEVEL_1, V4L2_MPEG_VIDEO_HEVC_LEVEL_1 },
778 { HFI_HEVC_LEVEL_2, V4L2_MPEG_VIDEO_HEVC_LEVEL_2 },
779 { HFI_HEVC_LEVEL_21, V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1 },
780 { HFI_HEVC_LEVEL_3, V4L2_MPEG_VIDEO_HEVC_LEVEL_3 },
781 { HFI_HEVC_LEVEL_31, V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1 },
782 { HFI_HEVC_LEVEL_4, V4L2_MPEG_VIDEO_HEVC_LEVEL_4 },
783 { HFI_HEVC_LEVEL_41, V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1 },
784 { HFI_HEVC_LEVEL_5, V4L2_MPEG_VIDEO_HEVC_LEVEL_5 },
785 { HFI_HEVC_LEVEL_51, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1 },
786 { HFI_HEVC_LEVEL_52, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2 },
787 { HFI_HEVC_LEVEL_6, V4L2_MPEG_VIDEO_HEVC_LEVEL_6 },
788 { HFI_HEVC_LEVEL_61, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1 },
789 { HFI_HEVC_LEVEL_62, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2 },
792 static const struct id_mapping vp8_profiles[] = {
793 { HFI_VPX_PROFILE_VERSION_0, V4L2_MPEG_VIDEO_VP8_PROFILE_0 },
794 { HFI_VPX_PROFILE_VERSION_1, V4L2_MPEG_VIDEO_VP8_PROFILE_1 },
795 { HFI_VPX_PROFILE_VERSION_2, V4L2_MPEG_VIDEO_VP8_PROFILE_2 },
796 { HFI_VPX_PROFILE_VERSION_3, V4L2_MPEG_VIDEO_VP8_PROFILE_3 },
799 static const struct id_mapping vp9_profiles[] = {
800 { HFI_VP9_PROFILE_P0, V4L2_MPEG_VIDEO_VP9_PROFILE_0 },
801 { HFI_VP9_PROFILE_P2_10B, V4L2_MPEG_VIDEO_VP9_PROFILE_2 },
804 static const struct id_mapping vp9_levels[] = {
805 { HFI_VP9_LEVEL_1, V4L2_MPEG_VIDEO_VP9_LEVEL_1_0 },
806 { HFI_VP9_LEVEL_11, V4L2_MPEG_VIDEO_VP9_LEVEL_1_1 },
807 { HFI_VP9_LEVEL_2, V4L2_MPEG_VIDEO_VP9_LEVEL_2_0},
808 { HFI_VP9_LEVEL_21, V4L2_MPEG_VIDEO_VP9_LEVEL_2_1 },
809 { HFI_VP9_LEVEL_3, V4L2_MPEG_VIDEO_VP9_LEVEL_3_0},
810 { HFI_VP9_LEVEL_31, V4L2_MPEG_VIDEO_VP9_LEVEL_3_1 },
811 { HFI_VP9_LEVEL_4, V4L2_MPEG_VIDEO_VP9_LEVEL_4_0 },
812 { HFI_VP9_LEVEL_41, V4L2_MPEG_VIDEO_VP9_LEVEL_4_1 },
813 { HFI_VP9_LEVEL_5, V4L2_MPEG_VIDEO_VP9_LEVEL_5_0 },
814 { HFI_VP9_LEVEL_51, V4L2_MPEG_VIDEO_VP9_LEVEL_5_1 },
815 { HFI_VP9_LEVEL_6, V4L2_MPEG_VIDEO_VP9_LEVEL_6_0 },
816 { HFI_VP9_LEVEL_61, V4L2_MPEG_VIDEO_VP9_LEVEL_6_1 },
819 static u32 find_v4l2_id(u32 hfi_id, const struct id_mapping *array, unsigned int array_sz)
823 if (!array || !array_sz)
826 for (i = 0; i < array_sz; i++)
827 if (hfi_id == array[i].hfi_id)
828 return array[i].v4l2_id;
833 static u32 find_hfi_id(u32 v4l2_id, const struct id_mapping *array, unsigned int array_sz)
837 if (!array || !array_sz)
840 for (i = 0; i < array_sz; i++)
841 if (v4l2_id == array[i].v4l2_id)
842 return array[i].hfi_id;
848 v4l2_id_profile_level(u32 hfi_codec, struct hfi_profile_level *pl, u32 *profile, u32 *level)
850 u32 hfi_pf = pl->profile;
851 u32 hfi_lvl = pl->level;
854 case HFI_VIDEO_CODEC_H264:
855 *profile = find_v4l2_id(hfi_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
856 *level = find_v4l2_id(hfi_lvl, h264_levels, ARRAY_SIZE(h264_levels));
858 case HFI_VIDEO_CODEC_MPEG2:
859 *profile = find_v4l2_id(hfi_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
860 *level = find_v4l2_id(hfi_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
862 case HFI_VIDEO_CODEC_MPEG4:
863 *profile = find_v4l2_id(hfi_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
864 *level = find_v4l2_id(hfi_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
866 case HFI_VIDEO_CODEC_VP8:
867 *profile = find_v4l2_id(hfi_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
870 case HFI_VIDEO_CODEC_VP9:
871 *profile = find_v4l2_id(hfi_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
872 *level = find_v4l2_id(hfi_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
874 case HFI_VIDEO_CODEC_HEVC:
875 *profile = find_v4l2_id(hfi_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
876 *level = find_v4l2_id(hfi_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
884 hfi_id_profile_level(u32 hfi_codec, u32 v4l2_pf, u32 v4l2_lvl, struct hfi_profile_level *pl)
887 case HFI_VIDEO_CODEC_H264:
888 pl->profile = find_hfi_id(v4l2_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
889 pl->level = find_hfi_id(v4l2_lvl, h264_levels, ARRAY_SIZE(h264_levels));
891 case HFI_VIDEO_CODEC_MPEG2:
892 pl->profile = find_hfi_id(v4l2_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
893 pl->level = find_hfi_id(v4l2_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
895 case HFI_VIDEO_CODEC_MPEG4:
896 pl->profile = find_hfi_id(v4l2_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
897 pl->level = find_hfi_id(v4l2_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
899 case HFI_VIDEO_CODEC_VP8:
900 pl->profile = find_hfi_id(v4l2_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
903 case HFI_VIDEO_CODEC_VP9:
904 pl->profile = find_hfi_id(v4l2_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
905 pl->level = find_hfi_id(v4l2_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
907 case HFI_VIDEO_CODEC_HEVC:
908 pl->profile = find_hfi_id(v4l2_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
909 pl->level = find_hfi_id(v4l2_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
916 int venus_helper_get_profile_level(struct venus_inst *inst, u32 *profile, u32 *level)
918 const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
919 union hfi_get_property hprop;
922 ret = hfi_session_get_property(inst, ptype, &hprop);
926 v4l2_id_profile_level(inst->hfi_codec, &hprop.profile_level, profile, level);
930 EXPORT_SYMBOL_GPL(venus_helper_get_profile_level);
932 int venus_helper_set_profile_level(struct venus_inst *inst, u32 profile, u32 level)
934 const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
935 struct hfi_profile_level pl;
937 hfi_id_profile_level(inst->hfi_codec, profile, level, &pl);
939 return hfi_session_set_property(inst, ptype, &pl);
941 EXPORT_SYMBOL_GPL(venus_helper_set_profile_level);
943 static u32 get_framesize_raw_nv12(u32 width, u32 height)
945 u32 y_stride, uv_stride, y_plane;
946 u32 y_sclines, uv_sclines, uv_plane;
949 y_stride = ALIGN(width, 128);
950 uv_stride = ALIGN(width, 128);
951 y_sclines = ALIGN(height, 32);
952 uv_sclines = ALIGN(((height + 1) >> 1), 16);
954 y_plane = y_stride * y_sclines;
955 uv_plane = uv_stride * uv_sclines + SZ_4K;
956 size = y_plane + uv_plane + SZ_8K;
958 return ALIGN(size, SZ_4K);
961 static u32 get_framesize_raw_nv12_ubwc(u32 width, u32 height)
963 u32 y_meta_stride, y_meta_plane;
964 u32 y_stride, y_plane;
965 u32 uv_meta_stride, uv_meta_plane;
966 u32 uv_stride, uv_plane;
967 u32 extradata = SZ_16K;
969 y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
970 y_meta_plane = y_meta_stride * ALIGN(DIV_ROUND_UP(height, 8), 16);
971 y_meta_plane = ALIGN(y_meta_plane, SZ_4K);
973 y_stride = ALIGN(width, 128);
974 y_plane = ALIGN(y_stride * ALIGN(height, 32), SZ_4K);
976 uv_meta_stride = ALIGN(DIV_ROUND_UP(width / 2, 16), 64);
977 uv_meta_plane = uv_meta_stride * ALIGN(DIV_ROUND_UP(height / 2, 8), 16);
978 uv_meta_plane = ALIGN(uv_meta_plane, SZ_4K);
980 uv_stride = ALIGN(width, 128);
981 uv_plane = ALIGN(uv_stride * ALIGN(height / 2, 32), SZ_4K);
983 return ALIGN(y_meta_plane + y_plane + uv_meta_plane + uv_plane +
984 max(extradata, y_stride * 48), SZ_4K);
987 static u32 get_framesize_raw_p010(u32 width, u32 height)
989 u32 y_plane, uv_plane, y_stride, uv_stride, y_sclines, uv_sclines;
991 y_stride = ALIGN(width * 2, 128);
992 uv_stride = ALIGN(width * 2, 128);
993 y_sclines = ALIGN(height, 32);
994 uv_sclines = ALIGN((height + 1) >> 1, 16);
995 y_plane = y_stride * y_sclines;
996 uv_plane = uv_stride * uv_sclines;
998 return ALIGN((y_plane + uv_plane), SZ_4K);
1001 static u32 get_framesize_raw_p010_ubwc(u32 width, u32 height)
1003 u32 y_stride, uv_stride, y_sclines, uv_sclines;
1004 u32 y_ubwc_plane, uv_ubwc_plane;
1005 u32 y_meta_stride, y_meta_scanlines;
1006 u32 uv_meta_stride, uv_meta_scanlines;
1007 u32 y_meta_plane, uv_meta_plane;
1010 y_stride = ALIGN(width * 2, 256);
1011 uv_stride = ALIGN(width * 2, 256);
1012 y_sclines = ALIGN(height, 16);
1013 uv_sclines = ALIGN((height + 1) >> 1, 16);
1015 y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
1016 uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
1017 y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
1018 y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
1019 y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
1020 uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 16), 64);
1021 uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
1022 uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
1024 size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
1026 return ALIGN(size, SZ_4K);
1029 static u32 get_framesize_raw_yuv420_tp10_ubwc(u32 width, u32 height)
1031 u32 y_stride, uv_stride, y_sclines, uv_sclines;
1032 u32 y_ubwc_plane, uv_ubwc_plane;
1033 u32 y_meta_stride, y_meta_scanlines;
1034 u32 uv_meta_stride, uv_meta_scanlines;
1035 u32 y_meta_plane, uv_meta_plane;
1036 u32 extradata = SZ_16K;
1039 y_stride = ALIGN(ALIGN(width, 192) * 4 / 3, 256);
1040 uv_stride = ALIGN(ALIGN(width, 192) * 4 / 3, 256);
1041 y_sclines = ALIGN(height, 16);
1042 uv_sclines = ALIGN((height + 1) >> 1, 16);
1044 y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
1045 uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
1046 y_meta_stride = ALIGN(DIV_ROUND_UP(width, 48), 64);
1047 y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
1048 y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
1049 uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 24), 64);
1050 uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
1051 uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
1053 size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
1054 size += max(extradata + SZ_8K, y_stride * 48);
1056 return ALIGN(size, SZ_4K);
1059 u32 venus_helper_get_framesz_raw(u32 hfi_fmt, u32 width, u32 height)
1062 case HFI_COLOR_FORMAT_NV12:
1063 case HFI_COLOR_FORMAT_NV21:
1064 return get_framesize_raw_nv12(width, height);
1065 case HFI_COLOR_FORMAT_NV12_UBWC:
1066 return get_framesize_raw_nv12_ubwc(width, height);
1067 case HFI_COLOR_FORMAT_P010:
1068 return get_framesize_raw_p010(width, height);
1069 case HFI_COLOR_FORMAT_P010_UBWC:
1070 return get_framesize_raw_p010_ubwc(width, height);
1071 case HFI_COLOR_FORMAT_YUV420_TP10_UBWC:
1072 return get_framesize_raw_yuv420_tp10_ubwc(width, height);
1077 EXPORT_SYMBOL_GPL(venus_helper_get_framesz_raw);
1079 u32 venus_helper_get_framesz(u32 v4l2_fmt, u32 width, u32 height)
1085 case V4L2_PIX_FMT_MPEG:
1086 case V4L2_PIX_FMT_H264:
1087 case V4L2_PIX_FMT_H264_NO_SC:
1088 case V4L2_PIX_FMT_H264_MVC:
1089 case V4L2_PIX_FMT_H263:
1090 case V4L2_PIX_FMT_MPEG1:
1091 case V4L2_PIX_FMT_MPEG2:
1092 case V4L2_PIX_FMT_MPEG4:
1093 case V4L2_PIX_FMT_XVID:
1094 case V4L2_PIX_FMT_VC1_ANNEX_G:
1095 case V4L2_PIX_FMT_VC1_ANNEX_L:
1096 case V4L2_PIX_FMT_VP8:
1097 case V4L2_PIX_FMT_VP9:
1098 case V4L2_PIX_FMT_HEVC:
1107 sz = ALIGN(height, 32) * ALIGN(width, 32) * 3 / 2 / 2;
1108 if (width < 1280 || height < 720)
1110 return ALIGN(sz, SZ_4K);
1113 hfi_fmt = to_hfi_raw_fmt(v4l2_fmt);
1117 return venus_helper_get_framesz_raw(hfi_fmt, width, height);
1119 EXPORT_SYMBOL_GPL(venus_helper_get_framesz);
1121 int venus_helper_set_input_resolution(struct venus_inst *inst,
1122 unsigned int width, unsigned int height)
1124 u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
1125 struct hfi_framesize fs;
1127 fs.buffer_type = HFI_BUFFER_INPUT;
1131 return hfi_session_set_property(inst, ptype, &fs);
1133 EXPORT_SYMBOL_GPL(venus_helper_set_input_resolution);
1135 int venus_helper_set_output_resolution(struct venus_inst *inst,
1136 unsigned int width, unsigned int height,
1139 u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
1140 struct hfi_framesize fs;
1142 fs.buffer_type = buftype;
1146 return hfi_session_set_property(inst, ptype, &fs);
1148 EXPORT_SYMBOL_GPL(venus_helper_set_output_resolution);
1150 static u32 venus_helper_get_work_mode(struct venus_inst *inst)
1155 mode = VIDC_WORK_MODE_2;
1156 if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
1157 num_mbs = (ALIGN(inst->height, 16) * ALIGN(inst->width, 16)) / 256;
1158 if (inst->hfi_codec == HFI_VIDEO_CODEC_MPEG2 ||
1159 inst->pic_struct != HFI_INTERLACE_FRAME_PROGRESSIVE ||
1160 num_mbs <= NUM_MBS_720P)
1161 mode = VIDC_WORK_MODE_1;
1163 num_mbs = (ALIGN(inst->out_height, 16) * ALIGN(inst->out_width, 16)) / 256;
1164 if (inst->hfi_codec == HFI_VIDEO_CODEC_VP8 &&
1165 num_mbs <= NUM_MBS_4K)
1166 mode = VIDC_WORK_MODE_1;
1172 int venus_helper_set_work_mode(struct venus_inst *inst)
1174 const u32 ptype = HFI_PROPERTY_PARAM_WORK_MODE;
1175 struct hfi_video_work_mode wm;
1178 if (!IS_V4(inst->core) && !IS_V6(inst->core))
1181 mode = venus_helper_get_work_mode(inst);
1182 wm.video_work_mode = mode;
1183 return hfi_session_set_property(inst, ptype, &wm);
1185 EXPORT_SYMBOL_GPL(venus_helper_set_work_mode);
1187 int venus_helper_set_format_constraints(struct venus_inst *inst)
1189 const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_PLANE_ACTUAL_CONSTRAINTS_INFO;
1190 struct hfi_uncompressed_plane_actual_constraints_info pconstraint;
1192 if (!IS_V6(inst->core))
1195 if (inst->opb_fmt == HFI_COLOR_FORMAT_NV12_UBWC ||
1196 inst->opb_fmt == HFI_COLOR_FORMAT_YUV420_TP10_UBWC)
1199 pconstraint.buffer_type = HFI_BUFFER_OUTPUT2;
1200 pconstraint.num_planes = 2;
1201 pconstraint.plane_format[0].stride_multiples = 128;
1202 pconstraint.plane_format[0].max_stride = 8192;
1203 pconstraint.plane_format[0].min_plane_buffer_height_multiple = 32;
1204 pconstraint.plane_format[0].buffer_alignment = 256;
1206 pconstraint.plane_format[1].stride_multiples = 128;
1207 pconstraint.plane_format[1].max_stride = 8192;
1208 pconstraint.plane_format[1].min_plane_buffer_height_multiple = 16;
1209 pconstraint.plane_format[1].buffer_alignment = 256;
1211 return hfi_session_set_property(inst, ptype, &pconstraint);
1213 EXPORT_SYMBOL_GPL(venus_helper_set_format_constraints);
1215 int venus_helper_set_num_bufs(struct venus_inst *inst, unsigned int input_bufs,
1216 unsigned int output_bufs,
1217 unsigned int output2_bufs)
1219 u32 ptype = HFI_PROPERTY_PARAM_BUFFER_COUNT_ACTUAL;
1220 struct hfi_buffer_count_actual buf_count;
1223 buf_count.type = HFI_BUFFER_INPUT;
1224 buf_count.count_actual = input_bufs;
1226 ret = hfi_session_set_property(inst, ptype, &buf_count);
1230 buf_count.type = HFI_BUFFER_OUTPUT;
1231 buf_count.count_actual = output_bufs;
1233 ret = hfi_session_set_property(inst, ptype, &buf_count);
1238 buf_count.type = HFI_BUFFER_OUTPUT2;
1239 buf_count.count_actual = output2_bufs;
1241 ret = hfi_session_set_property(inst, ptype, &buf_count);
1246 EXPORT_SYMBOL_GPL(venus_helper_set_num_bufs);
1248 int venus_helper_set_raw_format(struct venus_inst *inst, u32 hfi_format,
1251 const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_FORMAT_SELECT;
1252 struct hfi_uncompressed_format_select fmt;
1254 fmt.buffer_type = buftype;
1255 fmt.format = hfi_format;
1257 return hfi_session_set_property(inst, ptype, &fmt);
1259 EXPORT_SYMBOL_GPL(venus_helper_set_raw_format);
1261 int venus_helper_set_color_format(struct venus_inst *inst, u32 pixfmt)
1263 u32 hfi_format, buftype;
1265 if (inst->session_type == VIDC_SESSION_TYPE_DEC)
1266 buftype = HFI_BUFFER_OUTPUT;
1267 else if (inst->session_type == VIDC_SESSION_TYPE_ENC)
1268 buftype = HFI_BUFFER_INPUT;
1272 hfi_format = to_hfi_raw_fmt(pixfmt);
1276 return venus_helper_set_raw_format(inst, hfi_format, buftype);
1278 EXPORT_SYMBOL_GPL(venus_helper_set_color_format);
1280 int venus_helper_set_multistream(struct venus_inst *inst, bool out_en,
1283 struct hfi_multi_stream multi = {0};
1284 u32 ptype = HFI_PROPERTY_PARAM_VDEC_MULTI_STREAM;
1287 multi.buffer_type = HFI_BUFFER_OUTPUT;
1288 multi.enable = out_en;
1290 ret = hfi_session_set_property(inst, ptype, &multi);
1294 multi.buffer_type = HFI_BUFFER_OUTPUT2;
1295 multi.enable = out2_en;
1297 return hfi_session_set_property(inst, ptype, &multi);
1299 EXPORT_SYMBOL_GPL(venus_helper_set_multistream);
1301 int venus_helper_set_dyn_bufmode(struct venus_inst *inst)
1303 const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_ALLOC_MODE;
1304 struct hfi_buffer_alloc_mode mode;
1307 if (!is_dynamic_bufmode(inst))
1310 mode.type = HFI_BUFFER_OUTPUT;
1311 mode.mode = HFI_BUFFER_MODE_DYNAMIC;
1313 ret = hfi_session_set_property(inst, ptype, &mode);
1317 mode.type = HFI_BUFFER_OUTPUT2;
1319 return hfi_session_set_property(inst, ptype, &mode);
1321 EXPORT_SYMBOL_GPL(venus_helper_set_dyn_bufmode);
1323 int venus_helper_set_bufsize(struct venus_inst *inst, u32 bufsize, u32 buftype)
1325 const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_SIZE_ACTUAL;
1326 struct hfi_buffer_size_actual bufsz;
1328 bufsz.type = buftype;
1329 bufsz.size = bufsize;
1331 return hfi_session_set_property(inst, ptype, &bufsz);
1333 EXPORT_SYMBOL_GPL(venus_helper_set_bufsize);
1335 unsigned int venus_helper_get_opb_size(struct venus_inst *inst)
1337 /* the encoder has only one output */
1338 if (inst->session_type == VIDC_SESSION_TYPE_ENC)
1339 return inst->output_buf_size;
1341 if (inst->opb_buftype == HFI_BUFFER_OUTPUT)
1342 return inst->output_buf_size;
1343 else if (inst->opb_buftype == HFI_BUFFER_OUTPUT2)
1344 return inst->output2_buf_size;
1348 EXPORT_SYMBOL_GPL(venus_helper_get_opb_size);
1350 static void delayed_process_buf_func(struct work_struct *work)
1352 struct venus_buffer *buf, *n;
1353 struct venus_inst *inst;
1356 inst = container_of(work, struct venus_inst, delayed_process_work);
1358 mutex_lock(&inst->lock);
1360 if (!(inst->streamon_out & inst->streamon_cap))
1363 list_for_each_entry_safe(buf, n, &inst->delayed_process, ref_list) {
1364 if (buf->flags & HFI_BUFFERFLAG_READONLY)
1367 ret = session_process_buf(inst, &buf->vb);
1369 return_buf_error(inst, &buf->vb);
1371 list_del_init(&buf->ref_list);
1374 mutex_unlock(&inst->lock);
1377 void venus_helper_release_buf_ref(struct venus_inst *inst, unsigned int idx)
1379 struct venus_buffer *buf;
1381 list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
1382 if (buf->vb.vb2_buf.index == idx) {
1383 buf->flags &= ~HFI_BUFFERFLAG_READONLY;
1384 schedule_work(&inst->delayed_process_work);
1389 EXPORT_SYMBOL_GPL(venus_helper_release_buf_ref);
1391 void venus_helper_acquire_buf_ref(struct vb2_v4l2_buffer *vbuf)
1393 struct venus_buffer *buf = to_venus_buffer(vbuf);
1395 buf->flags |= HFI_BUFFERFLAG_READONLY;
1397 EXPORT_SYMBOL_GPL(venus_helper_acquire_buf_ref);
1399 static int is_buf_refed(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
1401 struct venus_buffer *buf = to_venus_buffer(vbuf);
1403 if (buf->flags & HFI_BUFFERFLAG_READONLY) {
1404 list_add_tail(&buf->ref_list, &inst->delayed_process);
1405 schedule_work(&inst->delayed_process_work);
1412 struct vb2_v4l2_buffer *
1413 venus_helper_find_buf(struct venus_inst *inst, unsigned int type, u32 idx)
1415 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1417 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1418 return v4l2_m2m_src_buf_remove_by_idx(m2m_ctx, idx);
1420 return v4l2_m2m_dst_buf_remove_by_idx(m2m_ctx, idx);
1422 EXPORT_SYMBOL_GPL(venus_helper_find_buf);
1424 void venus_helper_change_dpb_owner(struct venus_inst *inst,
1425 struct vb2_v4l2_buffer *vbuf, unsigned int type,
1426 unsigned int buf_type, u32 tag)
1428 struct intbuf *dpb_buf;
1430 if (!V4L2_TYPE_IS_CAPTURE(type) ||
1431 buf_type != inst->dpb_buftype)
1434 list_for_each_entry(dpb_buf, &inst->dpbbufs, list)
1435 if (dpb_buf->dpb_out_tag == tag) {
1436 dpb_buf->owned_by = DRIVER;
1440 EXPORT_SYMBOL_GPL(venus_helper_change_dpb_owner);
1442 int venus_helper_vb2_buf_init(struct vb2_buffer *vb)
1444 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1445 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1446 struct venus_buffer *buf = to_venus_buffer(vbuf);
1448 buf->size = vb2_plane_size(vb, 0);
1449 buf->dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1451 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1452 list_add_tail(&buf->reg_list, &inst->registeredbufs);
1456 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_init);
1458 int venus_helper_vb2_buf_prepare(struct vb2_buffer *vb)
1460 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1461 unsigned int out_buf_size = venus_helper_get_opb_size(inst);
1462 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1464 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1465 if (vbuf->field == V4L2_FIELD_ANY)
1466 vbuf->field = V4L2_FIELD_NONE;
1467 if (vbuf->field != V4L2_FIELD_NONE) {
1468 dev_err(inst->core->dev, "%s field isn't supported\n",
1474 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
1475 vb2_plane_size(vb, 0) < out_buf_size)
1477 if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1478 vb2_plane_size(vb, 0) < inst->input_buf_size)
1483 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_prepare);
1485 static void cache_payload(struct venus_inst *inst, struct vb2_buffer *vb)
1487 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1488 unsigned int idx = vbuf->vb2_buf.index;
1490 if (vbuf->vb2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1491 inst->payloads[idx] = vb2_get_plane_payload(vb, 0);
1494 void venus_helper_vb2_buf_queue(struct vb2_buffer *vb)
1496 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1497 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1498 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1501 v4l2_m2m_buf_queue(m2m_ctx, vbuf);
1503 /* Skip processing queued capture buffers after LAST flag */
1504 if (inst->session_type == VIDC_SESSION_TYPE_DEC &&
1505 V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1506 inst->codec_state == VENUS_DEC_STATE_DRC)
1509 cache_payload(inst, vb);
1511 if (inst->session_type == VIDC_SESSION_TYPE_ENC &&
1512 !(inst->streamon_out && inst->streamon_cap))
1515 if (vb2_start_streaming_called(vb->vb2_queue)) {
1516 ret = is_buf_refed(inst, vbuf);
1520 ret = session_process_buf(inst, vbuf);
1522 return_buf_error(inst, vbuf);
1525 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_queue);
1527 void venus_helper_buffers_done(struct venus_inst *inst, unsigned int type,
1528 enum vb2_buffer_state state)
1530 struct vb2_v4l2_buffer *buf;
1532 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1533 while ((buf = v4l2_m2m_src_buf_remove(inst->m2m_ctx)))
1534 v4l2_m2m_buf_done(buf, state);
1535 } else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1536 while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1537 v4l2_m2m_buf_done(buf, state);
1540 EXPORT_SYMBOL_GPL(venus_helper_buffers_done);
1542 void venus_helper_vb2_stop_streaming(struct vb2_queue *q)
1544 struct venus_inst *inst = vb2_get_drv_priv(q);
1545 struct venus_core *core = inst->core;
1548 mutex_lock(&inst->lock);
1550 if (inst->streamon_out & inst->streamon_cap) {
1551 ret = hfi_session_stop(inst);
1552 ret |= hfi_session_unload_res(inst);
1553 ret |= venus_helper_unregister_bufs(inst);
1554 ret |= venus_helper_intbufs_free(inst);
1555 ret |= hfi_session_deinit(inst);
1557 if (inst->session_error || test_bit(0, &core->sys_error))
1561 hfi_session_abort(inst);
1563 venus_helper_free_dpb_bufs(inst);
1565 venus_pm_load_scale(inst);
1566 INIT_LIST_HEAD(&inst->registeredbufs);
1569 venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
1570 VB2_BUF_STATE_ERROR);
1571 venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1572 VB2_BUF_STATE_ERROR);
1574 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1575 inst->streamon_out = 0;
1577 inst->streamon_cap = 0;
1579 venus_pm_release_core(inst);
1581 inst->session_error = 0;
1583 mutex_unlock(&inst->lock);
1585 EXPORT_SYMBOL_GPL(venus_helper_vb2_stop_streaming);
1587 void venus_helper_vb2_queue_error(struct venus_inst *inst)
1589 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1590 struct vb2_queue *q;
1592 q = v4l2_m2m_get_src_vq(m2m_ctx);
1594 q = v4l2_m2m_get_dst_vq(m2m_ctx);
1597 EXPORT_SYMBOL_GPL(venus_helper_vb2_queue_error);
1599 int venus_helper_process_initial_cap_bufs(struct venus_inst *inst)
1601 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1602 struct v4l2_m2m_buffer *buf, *n;
1605 v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
1606 ret = session_process_buf(inst, &buf->vb);
1608 return_buf_error(inst, &buf->vb);
1615 EXPORT_SYMBOL_GPL(venus_helper_process_initial_cap_bufs);
1617 int venus_helper_process_initial_out_bufs(struct venus_inst *inst)
1619 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1620 struct v4l2_m2m_buffer *buf, *n;
1623 v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
1624 ret = session_process_buf(inst, &buf->vb);
1626 return_buf_error(inst, &buf->vb);
1633 EXPORT_SYMBOL_GPL(venus_helper_process_initial_out_bufs);
1635 int venus_helper_vb2_start_streaming(struct venus_inst *inst)
1639 ret = venus_helper_intbufs_alloc(inst);
1643 ret = session_register_bufs(inst);
1647 venus_pm_load_scale(inst);
1649 ret = hfi_session_load_res(inst);
1651 goto err_unreg_bufs;
1653 ret = hfi_session_start(inst);
1655 goto err_unload_res;
1660 hfi_session_unload_res(inst);
1662 venus_helper_unregister_bufs(inst);
1664 venus_helper_intbufs_free(inst);
1667 EXPORT_SYMBOL_GPL(venus_helper_vb2_start_streaming);
1669 void venus_helper_m2m_device_run(void *priv)
1671 struct venus_inst *inst = priv;
1672 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1673 struct v4l2_m2m_buffer *buf, *n;
1676 mutex_lock(&inst->lock);
1678 v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
1679 ret = session_process_buf(inst, &buf->vb);
1681 return_buf_error(inst, &buf->vb);
1684 v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
1685 ret = session_process_buf(inst, &buf->vb);
1687 return_buf_error(inst, &buf->vb);
1690 mutex_unlock(&inst->lock);
1692 EXPORT_SYMBOL_GPL(venus_helper_m2m_device_run);
1694 void venus_helper_m2m_job_abort(void *priv)
1696 struct venus_inst *inst = priv;
1698 v4l2_m2m_job_finish(inst->m2m_dev, inst->m2m_ctx);
1700 EXPORT_SYMBOL_GPL(venus_helper_m2m_job_abort);
1702 int venus_helper_session_init(struct venus_inst *inst)
1704 enum hfi_version version = inst->core->res->hfi_version;
1705 u32 session_type = inst->session_type;
1709 codec = inst->session_type == VIDC_SESSION_TYPE_DEC ?
1710 inst->fmt_out->pixfmt : inst->fmt_cap->pixfmt;
1712 ret = hfi_session_init(inst, codec);
1716 inst->clk_data.vpp_freq = hfi_platform_get_codec_vpp_freq(version, codec,
1718 inst->clk_data.vsp_freq = hfi_platform_get_codec_vsp_freq(version, codec,
1720 inst->clk_data.low_power_freq = hfi_platform_get_codec_lp_freq(version, codec,
1725 EXPORT_SYMBOL_GPL(venus_helper_session_init);
1727 void venus_helper_init_instance(struct venus_inst *inst)
1729 if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
1730 INIT_LIST_HEAD(&inst->delayed_process);
1731 INIT_WORK(&inst->delayed_process_work,
1732 delayed_process_buf_func);
1735 EXPORT_SYMBOL_GPL(venus_helper_init_instance);
1737 static bool find_fmt_from_caps(struct hfi_plat_caps *caps, u32 buftype, u32 fmt)
1741 for (i = 0; i < caps->num_fmts; i++) {
1742 if (caps->fmts[i].buftype == buftype &&
1743 caps->fmts[i].fmt == fmt)
1750 int venus_helper_get_out_fmts(struct venus_inst *inst, u32 v4l2_fmt,
1751 u32 *out_fmt, u32 *out2_fmt, bool ubwc)
1753 struct venus_core *core = inst->core;
1754 struct hfi_plat_caps *caps;
1755 u32 ubwc_fmt, fmt = to_hfi_raw_fmt(v4l2_fmt);
1756 bool found, found_ubwc;
1758 *out_fmt = *out2_fmt = 0;
1763 caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
1768 ubwc_fmt = fmt | HFI_COLOR_FORMAT_UBWC_BASE;
1769 found_ubwc = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT,
1771 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1773 if (found_ubwc && found) {
1774 *out_fmt = ubwc_fmt;
1780 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT, fmt);
1787 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1796 EXPORT_SYMBOL_GPL(venus_helper_get_out_fmts);
1798 bool venus_helper_check_format(struct venus_inst *inst, u32 v4l2_pixfmt)
1800 struct venus_core *core = inst->core;
1801 u32 fmt = to_hfi_raw_fmt(v4l2_pixfmt);
1802 struct hfi_plat_caps *caps;
1808 caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
1812 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT, fmt);
1816 found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1820 EXPORT_SYMBOL_GPL(venus_helper_check_format);
1822 int venus_helper_set_stride(struct venus_inst *inst,
1823 unsigned int width, unsigned int height)
1825 const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_PLANE_ACTUAL_INFO;
1827 struct hfi_uncompressed_plane_actual_info plane_actual_info;
1829 plane_actual_info.buffer_type = HFI_BUFFER_INPUT;
1830 plane_actual_info.num_planes = 2;
1831 plane_actual_info.plane_format[0].actual_stride = width;
1832 plane_actual_info.plane_format[0].actual_plane_buffer_height = height;
1833 plane_actual_info.plane_format[1].actual_stride = width;
1834 plane_actual_info.plane_format[1].actual_plane_buffer_height = height / 2;
1836 return hfi_session_set_property(inst, ptype, &plane_actual_info);
1838 EXPORT_SYMBOL_GPL(venus_helper_set_stride);