18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
48c2ecf20Sopenharmony_ci * Copyright (C) 2017 Linaro Ltd.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include <linux/list.h>
78c2ecf20Sopenharmony_ci#include <linux/mutex.h>
88c2ecf20Sopenharmony_ci#include <linux/slab.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <media/videobuf2-dma-sg.h>
118c2ecf20Sopenharmony_ci#include <media/v4l2-mem2mem.h>
128c2ecf20Sopenharmony_ci#include <asm/div64.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "core.h"
158c2ecf20Sopenharmony_ci#include "helpers.h"
168c2ecf20Sopenharmony_ci#include "hfi_helper.h"
178c2ecf20Sopenharmony_ci#include "pm_helpers.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct intbuf {
208c2ecf20Sopenharmony_ci	struct list_head list;
218c2ecf20Sopenharmony_ci	u32 type;
228c2ecf20Sopenharmony_ci	size_t size;
238c2ecf20Sopenharmony_ci	void *va;
248c2ecf20Sopenharmony_ci	dma_addr_t da;
258c2ecf20Sopenharmony_ci	unsigned long attrs;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cibool venus_helper_check_codec(struct venus_inst *inst, u32 v4l2_pixfmt)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct venus_core *core = inst->core;
318c2ecf20Sopenharmony_ci	u32 session_type = inst->session_type;
328c2ecf20Sopenharmony_ci	u32 codec;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	switch (v4l2_pixfmt) {
358c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_H264:
368c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_H264;
378c2ecf20Sopenharmony_ci		break;
388c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_H263:
398c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_H263;
408c2ecf20Sopenharmony_ci		break;
418c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_MPEG1:
428c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_MPEG1;
438c2ecf20Sopenharmony_ci		break;
448c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_MPEG2:
458c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_MPEG2;
468c2ecf20Sopenharmony_ci		break;
478c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_MPEG4:
488c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_MPEG4;
498c2ecf20Sopenharmony_ci		break;
508c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VC1_ANNEX_G:
518c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VC1_ANNEX_L:
528c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_VC1;
538c2ecf20Sopenharmony_ci		break;
548c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VP8:
558c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_VP8;
568c2ecf20Sopenharmony_ci		break;
578c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VP9:
588c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_VP9;
598c2ecf20Sopenharmony_ci		break;
608c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_XVID:
618c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_DIVX;
628c2ecf20Sopenharmony_ci		break;
638c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_HEVC:
648c2ecf20Sopenharmony_ci		codec = HFI_VIDEO_CODEC_HEVC;
658c2ecf20Sopenharmony_ci		break;
668c2ecf20Sopenharmony_ci	default:
678c2ecf20Sopenharmony_ci		return false;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	if (session_type == VIDC_SESSION_TYPE_ENC && core->enc_codecs & codec)
718c2ecf20Sopenharmony_ci		return true;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if (session_type == VIDC_SESSION_TYPE_DEC && core->dec_codecs & codec)
748c2ecf20Sopenharmony_ci		return true;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return false;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_check_codec);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciint venus_helper_queue_dpb_bufs(struct venus_inst *inst)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	struct intbuf *buf;
838c2ecf20Sopenharmony_ci	int ret = 0;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	list_for_each_entry(buf, &inst->dpbbufs, list) {
868c2ecf20Sopenharmony_ci		struct hfi_frame_data fdata;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		memset(&fdata, 0, sizeof(fdata));
898c2ecf20Sopenharmony_ci		fdata.alloc_len = buf->size;
908c2ecf20Sopenharmony_ci		fdata.device_addr = buf->da;
918c2ecf20Sopenharmony_ci		fdata.buffer_type = buf->type;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci		ret = hfi_session_process_buf(inst, &fdata);
948c2ecf20Sopenharmony_ci		if (ret)
958c2ecf20Sopenharmony_ci			goto fail;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cifail:
998c2ecf20Sopenharmony_ci	return ret;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_queue_dpb_bufs);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciint venus_helper_free_dpb_bufs(struct venus_inst *inst)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct intbuf *buf, *n;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	list_for_each_entry_safe(buf, n, &inst->dpbbufs, list) {
1088c2ecf20Sopenharmony_ci		list_del_init(&buf->list);
1098c2ecf20Sopenharmony_ci		dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
1108c2ecf20Sopenharmony_ci			       buf->attrs);
1118c2ecf20Sopenharmony_ci		kfree(buf);
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&inst->dpbbufs);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	return 0;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_free_dpb_bufs);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciint venus_helper_alloc_dpb_bufs(struct venus_inst *inst)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	struct venus_core *core = inst->core;
1238c2ecf20Sopenharmony_ci	struct device *dev = core->dev;
1248c2ecf20Sopenharmony_ci	enum hfi_version ver = core->res->hfi_version;
1258c2ecf20Sopenharmony_ci	struct hfi_buffer_requirements bufreq;
1268c2ecf20Sopenharmony_ci	u32 buftype = inst->dpb_buftype;
1278c2ecf20Sopenharmony_ci	unsigned int dpb_size = 0;
1288c2ecf20Sopenharmony_ci	struct intbuf *buf;
1298c2ecf20Sopenharmony_ci	unsigned int i;
1308c2ecf20Sopenharmony_ci	u32 count;
1318c2ecf20Sopenharmony_ci	int ret;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* no need to allocate dpb buffers */
1348c2ecf20Sopenharmony_ci	if (!inst->dpb_fmt)
1358c2ecf20Sopenharmony_ci		return 0;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	if (inst->dpb_buftype == HFI_BUFFER_OUTPUT)
1388c2ecf20Sopenharmony_ci		dpb_size = inst->output_buf_size;
1398c2ecf20Sopenharmony_ci	else if (inst->dpb_buftype == HFI_BUFFER_OUTPUT2)
1408c2ecf20Sopenharmony_ci		dpb_size = inst->output2_buf_size;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (!dpb_size)
1438c2ecf20Sopenharmony_ci		return 0;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	ret = venus_helper_get_bufreq(inst, buftype, &bufreq);
1468c2ecf20Sopenharmony_ci	if (ret)
1478c2ecf20Sopenharmony_ci		return ret;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	count = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
1528c2ecf20Sopenharmony_ci		buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1538c2ecf20Sopenharmony_ci		if (!buf) {
1548c2ecf20Sopenharmony_ci			ret = -ENOMEM;
1558c2ecf20Sopenharmony_ci			goto fail;
1568c2ecf20Sopenharmony_ci		}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci		buf->type = buftype;
1598c2ecf20Sopenharmony_ci		buf->size = dpb_size;
1608c2ecf20Sopenharmony_ci		buf->attrs = DMA_ATTR_WRITE_COMBINE |
1618c2ecf20Sopenharmony_ci			     DMA_ATTR_NO_KERNEL_MAPPING;
1628c2ecf20Sopenharmony_ci		buf->va = dma_alloc_attrs(dev, buf->size, &buf->da, GFP_KERNEL,
1638c2ecf20Sopenharmony_ci					  buf->attrs);
1648c2ecf20Sopenharmony_ci		if (!buf->va) {
1658c2ecf20Sopenharmony_ci			kfree(buf);
1668c2ecf20Sopenharmony_ci			ret = -ENOMEM;
1678c2ecf20Sopenharmony_ci			goto fail;
1688c2ecf20Sopenharmony_ci		}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		list_add_tail(&buf->list, &inst->dpbbufs);
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	return 0;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cifail:
1768c2ecf20Sopenharmony_ci	venus_helper_free_dpb_bufs(inst);
1778c2ecf20Sopenharmony_ci	return ret;
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_alloc_dpb_bufs);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic int intbufs_set_buffer(struct venus_inst *inst, u32 type)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	struct venus_core *core = inst->core;
1848c2ecf20Sopenharmony_ci	struct device *dev = core->dev;
1858c2ecf20Sopenharmony_ci	struct hfi_buffer_requirements bufreq;
1868c2ecf20Sopenharmony_ci	struct hfi_buffer_desc bd;
1878c2ecf20Sopenharmony_ci	struct intbuf *buf;
1888c2ecf20Sopenharmony_ci	unsigned int i;
1898c2ecf20Sopenharmony_ci	int ret;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	ret = venus_helper_get_bufreq(inst, type, &bufreq);
1928c2ecf20Sopenharmony_ci	if (ret)
1938c2ecf20Sopenharmony_ci		return 0;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	if (!bufreq.size)
1968c2ecf20Sopenharmony_ci		return 0;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	for (i = 0; i < bufreq.count_actual; i++) {
1998c2ecf20Sopenharmony_ci		buf = kzalloc(sizeof(*buf), GFP_KERNEL);
2008c2ecf20Sopenharmony_ci		if (!buf) {
2018c2ecf20Sopenharmony_ci			ret = -ENOMEM;
2028c2ecf20Sopenharmony_ci			goto fail;
2038c2ecf20Sopenharmony_ci		}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci		buf->type = bufreq.type;
2068c2ecf20Sopenharmony_ci		buf->size = bufreq.size;
2078c2ecf20Sopenharmony_ci		buf->attrs = DMA_ATTR_WRITE_COMBINE |
2088c2ecf20Sopenharmony_ci			     DMA_ATTR_NO_KERNEL_MAPPING;
2098c2ecf20Sopenharmony_ci		buf->va = dma_alloc_attrs(dev, buf->size, &buf->da, GFP_KERNEL,
2108c2ecf20Sopenharmony_ci					  buf->attrs);
2118c2ecf20Sopenharmony_ci		if (!buf->va) {
2128c2ecf20Sopenharmony_ci			ret = -ENOMEM;
2138c2ecf20Sopenharmony_ci			goto fail;
2148c2ecf20Sopenharmony_ci		}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci		memset(&bd, 0, sizeof(bd));
2178c2ecf20Sopenharmony_ci		bd.buffer_size = buf->size;
2188c2ecf20Sopenharmony_ci		bd.buffer_type = buf->type;
2198c2ecf20Sopenharmony_ci		bd.num_buffers = 1;
2208c2ecf20Sopenharmony_ci		bd.device_addr = buf->da;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci		ret = hfi_session_set_buffers(inst, &bd);
2238c2ecf20Sopenharmony_ci		if (ret) {
2248c2ecf20Sopenharmony_ci			dev_err(dev, "set session buffers failed\n");
2258c2ecf20Sopenharmony_ci			goto dma_free;
2268c2ecf20Sopenharmony_ci		}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci		list_add_tail(&buf->list, &inst->internalbufs);
2298c2ecf20Sopenharmony_ci	}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	return 0;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cidma_free:
2348c2ecf20Sopenharmony_ci	dma_free_attrs(dev, buf->size, buf->va, buf->da, buf->attrs);
2358c2ecf20Sopenharmony_cifail:
2368c2ecf20Sopenharmony_ci	kfree(buf);
2378c2ecf20Sopenharmony_ci	return ret;
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic int intbufs_unset_buffers(struct venus_inst *inst)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	struct hfi_buffer_desc bd = {0};
2438c2ecf20Sopenharmony_ci	struct intbuf *buf, *n;
2448c2ecf20Sopenharmony_ci	int ret = 0;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	list_for_each_entry_safe(buf, n, &inst->internalbufs, list) {
2478c2ecf20Sopenharmony_ci		bd.buffer_size = buf->size;
2488c2ecf20Sopenharmony_ci		bd.buffer_type = buf->type;
2498c2ecf20Sopenharmony_ci		bd.num_buffers = 1;
2508c2ecf20Sopenharmony_ci		bd.device_addr = buf->da;
2518c2ecf20Sopenharmony_ci		bd.response_required = true;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci		ret = hfi_session_unset_buffers(inst, &bd);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci		list_del_init(&buf->list);
2568c2ecf20Sopenharmony_ci		dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
2578c2ecf20Sopenharmony_ci			       buf->attrs);
2588c2ecf20Sopenharmony_ci		kfree(buf);
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	return ret;
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic const unsigned int intbuf_types_1xx[] = {
2658c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_1XX),
2668c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_1XX),
2678c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_1XX),
2688c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_PERSIST,
2698c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_PERSIST_1,
2708c2ecf20Sopenharmony_ci};
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic const unsigned int intbuf_types_4xx[] = {
2738c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_4XX),
2748c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_4XX),
2758c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_4XX),
2768c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_PERSIST,
2778c2ecf20Sopenharmony_ci	HFI_BUFFER_INTERNAL_PERSIST_1,
2788c2ecf20Sopenharmony_ci};
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ciint venus_helper_intbufs_alloc(struct venus_inst *inst)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	const unsigned int *intbuf;
2838c2ecf20Sopenharmony_ci	size_t arr_sz, i;
2848c2ecf20Sopenharmony_ci	int ret;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (IS_V4(inst->core)) {
2878c2ecf20Sopenharmony_ci		arr_sz = ARRAY_SIZE(intbuf_types_4xx);
2888c2ecf20Sopenharmony_ci		intbuf = intbuf_types_4xx;
2898c2ecf20Sopenharmony_ci	} else {
2908c2ecf20Sopenharmony_ci		arr_sz = ARRAY_SIZE(intbuf_types_1xx);
2918c2ecf20Sopenharmony_ci		intbuf = intbuf_types_1xx;
2928c2ecf20Sopenharmony_ci	}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	for (i = 0; i < arr_sz; i++) {
2958c2ecf20Sopenharmony_ci		ret = intbufs_set_buffer(inst, intbuf[i]);
2968c2ecf20Sopenharmony_ci		if (ret)
2978c2ecf20Sopenharmony_ci			goto error;
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	return 0;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cierror:
3038c2ecf20Sopenharmony_ci	intbufs_unset_buffers(inst);
3048c2ecf20Sopenharmony_ci	return ret;
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_intbufs_alloc);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ciint venus_helper_intbufs_free(struct venus_inst *inst)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	return intbufs_unset_buffers(inst);
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_intbufs_free);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ciint venus_helper_intbufs_realloc(struct venus_inst *inst)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	enum hfi_version ver = inst->core->res->hfi_version;
3178c2ecf20Sopenharmony_ci	struct hfi_buffer_desc bd;
3188c2ecf20Sopenharmony_ci	struct intbuf *buf, *n;
3198c2ecf20Sopenharmony_ci	int ret;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	list_for_each_entry_safe(buf, n, &inst->internalbufs, list) {
3228c2ecf20Sopenharmony_ci		if (buf->type == HFI_BUFFER_INTERNAL_PERSIST ||
3238c2ecf20Sopenharmony_ci		    buf->type == HFI_BUFFER_INTERNAL_PERSIST_1)
3248c2ecf20Sopenharmony_ci			continue;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci		memset(&bd, 0, sizeof(bd));
3278c2ecf20Sopenharmony_ci		bd.buffer_size = buf->size;
3288c2ecf20Sopenharmony_ci		bd.buffer_type = buf->type;
3298c2ecf20Sopenharmony_ci		bd.num_buffers = 1;
3308c2ecf20Sopenharmony_ci		bd.device_addr = buf->da;
3318c2ecf20Sopenharmony_ci		bd.response_required = true;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci		ret = hfi_session_unset_buffers(inst, &bd);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
3368c2ecf20Sopenharmony_ci			       buf->attrs);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci		list_del_init(&buf->list);
3398c2ecf20Sopenharmony_ci		kfree(buf);
3408c2ecf20Sopenharmony_ci	}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH(ver));
3438c2ecf20Sopenharmony_ci	if (ret)
3448c2ecf20Sopenharmony_ci		goto err;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH_1(ver));
3478c2ecf20Sopenharmony_ci	if (ret)
3488c2ecf20Sopenharmony_ci		goto err;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH_2(ver));
3518c2ecf20Sopenharmony_ci	if (ret)
3528c2ecf20Sopenharmony_ci		goto err;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	return 0;
3558c2ecf20Sopenharmony_cierr:
3568c2ecf20Sopenharmony_ci	return ret;
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_intbufs_realloc);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic void fill_buffer_desc(const struct venus_buffer *buf,
3618c2ecf20Sopenharmony_ci			     struct hfi_buffer_desc *bd, bool response)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	memset(bd, 0, sizeof(*bd));
3648c2ecf20Sopenharmony_ci	bd->buffer_type = HFI_BUFFER_OUTPUT;
3658c2ecf20Sopenharmony_ci	bd->buffer_size = buf->size;
3668c2ecf20Sopenharmony_ci	bd->num_buffers = 1;
3678c2ecf20Sopenharmony_ci	bd->device_addr = buf->dma_addr;
3688c2ecf20Sopenharmony_ci	bd->response_required = response;
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic void return_buf_error(struct venus_inst *inst,
3728c2ecf20Sopenharmony_ci			     struct vb2_v4l2_buffer *vbuf)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	if (vbuf->vb2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
3778c2ecf20Sopenharmony_ci		v4l2_m2m_src_buf_remove_by_buf(m2m_ctx, vbuf);
3788c2ecf20Sopenharmony_ci	else
3798c2ecf20Sopenharmony_ci		v4l2_m2m_dst_buf_remove_by_buf(m2m_ctx, vbuf);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic void
3858c2ecf20Sopenharmony_ciput_ts_metadata(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	struct vb2_buffer *vb = &vbuf->vb2_buf;
3888c2ecf20Sopenharmony_ci	unsigned int i;
3898c2ecf20Sopenharmony_ci	int slot = -1;
3908c2ecf20Sopenharmony_ci	u64 ts_us = vb->timestamp;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(inst->tss); i++) {
3938c2ecf20Sopenharmony_ci		if (!inst->tss[i].used) {
3948c2ecf20Sopenharmony_ci			slot = i;
3958c2ecf20Sopenharmony_ci			break;
3968c2ecf20Sopenharmony_ci		}
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	if (slot == -1) {
4008c2ecf20Sopenharmony_ci		dev_dbg(inst->core->dev, VDBGL "no free slot\n");
4018c2ecf20Sopenharmony_ci		return;
4028c2ecf20Sopenharmony_ci	}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	do_div(ts_us, NSEC_PER_USEC);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	inst->tss[slot].used = true;
4078c2ecf20Sopenharmony_ci	inst->tss[slot].flags = vbuf->flags;
4088c2ecf20Sopenharmony_ci	inst->tss[slot].tc = vbuf->timecode;
4098c2ecf20Sopenharmony_ci	inst->tss[slot].ts_us = ts_us;
4108c2ecf20Sopenharmony_ci	inst->tss[slot].ts_ns = vb->timestamp;
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_civoid venus_helper_get_ts_metadata(struct venus_inst *inst, u64 timestamp_us,
4148c2ecf20Sopenharmony_ci				  struct vb2_v4l2_buffer *vbuf)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	struct vb2_buffer *vb = &vbuf->vb2_buf;
4178c2ecf20Sopenharmony_ci	unsigned int i;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(inst->tss); ++i) {
4208c2ecf20Sopenharmony_ci		if (!inst->tss[i].used)
4218c2ecf20Sopenharmony_ci			continue;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci		if (inst->tss[i].ts_us != timestamp_us)
4248c2ecf20Sopenharmony_ci			continue;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci		inst->tss[i].used = false;
4278c2ecf20Sopenharmony_ci		vbuf->flags |= inst->tss[i].flags;
4288c2ecf20Sopenharmony_ci		vbuf->timecode = inst->tss[i].tc;
4298c2ecf20Sopenharmony_ci		vb->timestamp = inst->tss[i].ts_ns;
4308c2ecf20Sopenharmony_ci		break;
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_get_ts_metadata);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic int
4368c2ecf20Sopenharmony_cisession_process_buf(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct venus_buffer *buf = to_venus_buffer(vbuf);
4398c2ecf20Sopenharmony_ci	struct vb2_buffer *vb = &vbuf->vb2_buf;
4408c2ecf20Sopenharmony_ci	unsigned int type = vb->type;
4418c2ecf20Sopenharmony_ci	struct hfi_frame_data fdata;
4428c2ecf20Sopenharmony_ci	int ret;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	memset(&fdata, 0, sizeof(fdata));
4458c2ecf20Sopenharmony_ci	fdata.alloc_len = buf->size;
4468c2ecf20Sopenharmony_ci	fdata.device_addr = buf->dma_addr;
4478c2ecf20Sopenharmony_ci	fdata.timestamp = vb->timestamp;
4488c2ecf20Sopenharmony_ci	do_div(fdata.timestamp, NSEC_PER_USEC);
4498c2ecf20Sopenharmony_ci	fdata.flags = 0;
4508c2ecf20Sopenharmony_ci	fdata.clnt_data = vbuf->vb2_buf.index;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
4538c2ecf20Sopenharmony_ci		fdata.buffer_type = HFI_BUFFER_INPUT;
4548c2ecf20Sopenharmony_ci		fdata.filled_len = vb2_get_plane_payload(vb, 0);
4558c2ecf20Sopenharmony_ci		fdata.offset = vb->planes[0].data_offset;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci		if (vbuf->flags & V4L2_BUF_FLAG_LAST || !fdata.filled_len)
4588c2ecf20Sopenharmony_ci			fdata.flags |= HFI_BUFFERFLAG_EOS;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci		if (inst->session_type == VIDC_SESSION_TYPE_DEC)
4618c2ecf20Sopenharmony_ci			put_ts_metadata(inst, vbuf);
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci		venus_pm_load_scale(inst);
4648c2ecf20Sopenharmony_ci	} else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
4658c2ecf20Sopenharmony_ci		if (inst->session_type == VIDC_SESSION_TYPE_ENC)
4668c2ecf20Sopenharmony_ci			fdata.buffer_type = HFI_BUFFER_OUTPUT;
4678c2ecf20Sopenharmony_ci		else
4688c2ecf20Sopenharmony_ci			fdata.buffer_type = inst->opb_buftype;
4698c2ecf20Sopenharmony_ci		fdata.filled_len = 0;
4708c2ecf20Sopenharmony_ci		fdata.offset = 0;
4718c2ecf20Sopenharmony_ci	}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	ret = hfi_session_process_buf(inst, &fdata);
4748c2ecf20Sopenharmony_ci	if (ret)
4758c2ecf20Sopenharmony_ci		return ret;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	return 0;
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_cistatic bool is_dynamic_bufmode(struct venus_inst *inst)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	struct venus_core *core = inst->core;
4838c2ecf20Sopenharmony_ci	struct venus_caps *caps;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	/*
4868c2ecf20Sopenharmony_ci	 * v4 doesn't send BUFFER_ALLOC_MODE_SUPPORTED property and supports
4878c2ecf20Sopenharmony_ci	 * dynamic buffer mode by default for HFI_BUFFER_OUTPUT/OUTPUT2.
4888c2ecf20Sopenharmony_ci	 */
4898c2ecf20Sopenharmony_ci	if (IS_V4(core))
4908c2ecf20Sopenharmony_ci		return true;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
4938c2ecf20Sopenharmony_ci	if (!caps)
4948c2ecf20Sopenharmony_ci		return false;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	return caps->cap_bufs_mode_dynamic;
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ciint venus_helper_unregister_bufs(struct venus_inst *inst)
5008c2ecf20Sopenharmony_ci{
5018c2ecf20Sopenharmony_ci	struct venus_buffer *buf, *n;
5028c2ecf20Sopenharmony_ci	struct hfi_buffer_desc bd;
5038c2ecf20Sopenharmony_ci	int ret = 0;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	if (is_dynamic_bufmode(inst))
5068c2ecf20Sopenharmony_ci		return 0;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	list_for_each_entry_safe(buf, n, &inst->registeredbufs, reg_list) {
5098c2ecf20Sopenharmony_ci		fill_buffer_desc(buf, &bd, true);
5108c2ecf20Sopenharmony_ci		ret = hfi_session_unset_buffers(inst, &bd);
5118c2ecf20Sopenharmony_ci		list_del_init(&buf->reg_list);
5128c2ecf20Sopenharmony_ci	}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	return ret;
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_unregister_bufs);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic int session_register_bufs(struct venus_inst *inst)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	struct venus_core *core = inst->core;
5218c2ecf20Sopenharmony_ci	struct device *dev = core->dev;
5228c2ecf20Sopenharmony_ci	struct hfi_buffer_desc bd;
5238c2ecf20Sopenharmony_ci	struct venus_buffer *buf;
5248c2ecf20Sopenharmony_ci	int ret = 0;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	if (is_dynamic_bufmode(inst))
5278c2ecf20Sopenharmony_ci		return 0;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
5308c2ecf20Sopenharmony_ci		fill_buffer_desc(buf, &bd, false);
5318c2ecf20Sopenharmony_ci		ret = hfi_session_set_buffers(inst, &bd);
5328c2ecf20Sopenharmony_ci		if (ret) {
5338c2ecf20Sopenharmony_ci			dev_err(dev, "%s: set buffer failed\n", __func__);
5348c2ecf20Sopenharmony_ci			break;
5358c2ecf20Sopenharmony_ci		}
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	return ret;
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic u32 to_hfi_raw_fmt(u32 v4l2_fmt)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	switch (v4l2_fmt) {
5448c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
5458c2ecf20Sopenharmony_ci		return HFI_COLOR_FORMAT_NV12;
5468c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
5478c2ecf20Sopenharmony_ci		return HFI_COLOR_FORMAT_NV21;
5488c2ecf20Sopenharmony_ci	default:
5498c2ecf20Sopenharmony_ci		break;
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	return 0;
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ciint venus_helper_get_bufreq(struct venus_inst *inst, u32 type,
5568c2ecf20Sopenharmony_ci			    struct hfi_buffer_requirements *req)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	u32 ptype = HFI_PROPERTY_CONFIG_BUFFER_REQUIREMENTS;
5598c2ecf20Sopenharmony_ci	union hfi_get_property hprop;
5608c2ecf20Sopenharmony_ci	unsigned int i;
5618c2ecf20Sopenharmony_ci	int ret;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	if (req)
5648c2ecf20Sopenharmony_ci		memset(req, 0, sizeof(*req));
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	ret = hfi_session_get_property(inst, ptype, &hprop);
5678c2ecf20Sopenharmony_ci	if (ret)
5688c2ecf20Sopenharmony_ci		return ret;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	ret = -EINVAL;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	for (i = 0; i < HFI_BUFFER_TYPE_MAX; i++) {
5738c2ecf20Sopenharmony_ci		if (hprop.bufreq[i].type != type)
5748c2ecf20Sopenharmony_ci			continue;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci		if (req)
5778c2ecf20Sopenharmony_ci			memcpy(req, &hprop.bufreq[i], sizeof(*req));
5788c2ecf20Sopenharmony_ci		ret = 0;
5798c2ecf20Sopenharmony_ci		break;
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	return ret;
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_get_bufreq);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_cistruct id_mapping {
5878c2ecf20Sopenharmony_ci	u32 hfi_id;
5888c2ecf20Sopenharmony_ci	u32 v4l2_id;
5898c2ecf20Sopenharmony_ci};
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_cistatic const struct id_mapping mpeg4_profiles[] = {
5928c2ecf20Sopenharmony_ci	{ HFI_MPEG4_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE },
5938c2ecf20Sopenharmony_ci	{ HFI_MPEG4_PROFILE_ADVANCEDSIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE },
5948c2ecf20Sopenharmony_ci};
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_cistatic const struct id_mapping mpeg4_levels[] = {
5978c2ecf20Sopenharmony_ci	{ HFI_MPEG4_LEVEL_0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 },
5988c2ecf20Sopenharmony_ci	{ HFI_MPEG4_LEVEL_0b, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B },
5998c2ecf20Sopenharmony_ci	{ HFI_MPEG4_LEVEL_1, V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 },
6008c2ecf20Sopenharmony_ci	{ HFI_MPEG4_LEVEL_2, V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 },
6018c2ecf20Sopenharmony_ci	{ HFI_MPEG4_LEVEL_3, V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 },
6028c2ecf20Sopenharmony_ci	{ HFI_MPEG4_LEVEL_4, V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 },
6038c2ecf20Sopenharmony_ci	{ HFI_MPEG4_LEVEL_5, V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 },
6048c2ecf20Sopenharmony_ci};
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_cistatic const struct id_mapping mpeg2_profiles[] = {
6078c2ecf20Sopenharmony_ci	{ HFI_MPEG2_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SIMPLE },
6088c2ecf20Sopenharmony_ci	{ HFI_MPEG2_PROFILE_MAIN, V4L2_MPEG_VIDEO_MPEG2_PROFILE_MAIN },
6098c2ecf20Sopenharmony_ci	{ HFI_MPEG2_PROFILE_SNR, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SNR_SCALABLE },
6108c2ecf20Sopenharmony_ci	{ HFI_MPEG2_PROFILE_SPATIAL, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SPATIALLY_SCALABLE },
6118c2ecf20Sopenharmony_ci	{ HFI_MPEG2_PROFILE_HIGH, V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH },
6128c2ecf20Sopenharmony_ci};
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic const struct id_mapping mpeg2_levels[] = {
6158c2ecf20Sopenharmony_ci	{ HFI_MPEG2_LEVEL_LL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_LOW },
6168c2ecf20Sopenharmony_ci	{ HFI_MPEG2_LEVEL_ML, V4L2_MPEG_VIDEO_MPEG2_LEVEL_MAIN },
6178c2ecf20Sopenharmony_ci	{ HFI_MPEG2_LEVEL_H14, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH_1440 },
6188c2ecf20Sopenharmony_ci	{ HFI_MPEG2_LEVEL_HL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH },
6198c2ecf20Sopenharmony_ci};
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_cistatic const struct id_mapping h264_profiles[] = {
6228c2ecf20Sopenharmony_ci	{ HFI_H264_PROFILE_BASELINE, V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE },
6238c2ecf20Sopenharmony_ci	{ HFI_H264_PROFILE_MAIN, V4L2_MPEG_VIDEO_H264_PROFILE_MAIN },
6248c2ecf20Sopenharmony_ci	{ HFI_H264_PROFILE_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH },
6258c2ecf20Sopenharmony_ci	{ HFI_H264_PROFILE_STEREO_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH },
6268c2ecf20Sopenharmony_ci	{ HFI_H264_PROFILE_MULTIVIEW_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH },
6278c2ecf20Sopenharmony_ci	{ HFI_H264_PROFILE_CONSTRAINED_BASE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE },
6288c2ecf20Sopenharmony_ci	{ HFI_H264_PROFILE_CONSTRAINED_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH },
6298c2ecf20Sopenharmony_ci};
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_cistatic const struct id_mapping h264_levels[] = {
6328c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_1, V4L2_MPEG_VIDEO_H264_LEVEL_1_0 },
6338c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_1b, V4L2_MPEG_VIDEO_H264_LEVEL_1B },
6348c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_11, V4L2_MPEG_VIDEO_H264_LEVEL_1_1 },
6358c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_12, V4L2_MPEG_VIDEO_H264_LEVEL_1_2 },
6368c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_13, V4L2_MPEG_VIDEO_H264_LEVEL_1_3 },
6378c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_2, V4L2_MPEG_VIDEO_H264_LEVEL_2_0 },
6388c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_21, V4L2_MPEG_VIDEO_H264_LEVEL_2_1 },
6398c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_22, V4L2_MPEG_VIDEO_H264_LEVEL_2_2 },
6408c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_3, V4L2_MPEG_VIDEO_H264_LEVEL_3_0 },
6418c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_31, V4L2_MPEG_VIDEO_H264_LEVEL_3_1 },
6428c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_32, V4L2_MPEG_VIDEO_H264_LEVEL_3_2 },
6438c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_4, V4L2_MPEG_VIDEO_H264_LEVEL_4_0 },
6448c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_41, V4L2_MPEG_VIDEO_H264_LEVEL_4_1 },
6458c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_42, V4L2_MPEG_VIDEO_H264_LEVEL_4_2 },
6468c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_5, V4L2_MPEG_VIDEO_H264_LEVEL_5_0 },
6478c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_51, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
6488c2ecf20Sopenharmony_ci	{ HFI_H264_LEVEL_52, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
6498c2ecf20Sopenharmony_ci};
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_cistatic const struct id_mapping hevc_profiles[] = {
6528c2ecf20Sopenharmony_ci	{ HFI_HEVC_PROFILE_MAIN, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN },
6538c2ecf20Sopenharmony_ci	{ HFI_HEVC_PROFILE_MAIN_STILL_PIC, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE },
6548c2ecf20Sopenharmony_ci	{ HFI_HEVC_PROFILE_MAIN10, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10 },
6558c2ecf20Sopenharmony_ci};
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_cistatic const struct id_mapping hevc_levels[] = {
6588c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_1, V4L2_MPEG_VIDEO_HEVC_LEVEL_1 },
6598c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_2, V4L2_MPEG_VIDEO_HEVC_LEVEL_2 },
6608c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_21, V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1 },
6618c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_3, V4L2_MPEG_VIDEO_HEVC_LEVEL_3 },
6628c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_31, V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1 },
6638c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_4, V4L2_MPEG_VIDEO_HEVC_LEVEL_4 },
6648c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_41, V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1 },
6658c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_5, V4L2_MPEG_VIDEO_HEVC_LEVEL_5 },
6668c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_51, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1 },
6678c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_52, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2 },
6688c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_6, V4L2_MPEG_VIDEO_HEVC_LEVEL_6 },
6698c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_61, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1 },
6708c2ecf20Sopenharmony_ci	{ HFI_HEVC_LEVEL_62, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2 },
6718c2ecf20Sopenharmony_ci};
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_cistatic const struct id_mapping vp8_profiles[] = {
6748c2ecf20Sopenharmony_ci	{ HFI_VPX_PROFILE_VERSION_0, V4L2_MPEG_VIDEO_VP8_PROFILE_0 },
6758c2ecf20Sopenharmony_ci	{ HFI_VPX_PROFILE_VERSION_1, V4L2_MPEG_VIDEO_VP8_PROFILE_1 },
6768c2ecf20Sopenharmony_ci	{ HFI_VPX_PROFILE_VERSION_2, V4L2_MPEG_VIDEO_VP8_PROFILE_2 },
6778c2ecf20Sopenharmony_ci	{ HFI_VPX_PROFILE_VERSION_3, V4L2_MPEG_VIDEO_VP8_PROFILE_3 },
6788c2ecf20Sopenharmony_ci};
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_cistatic const struct id_mapping vp9_profiles[] = {
6818c2ecf20Sopenharmony_ci	{ HFI_VP9_PROFILE_P0, V4L2_MPEG_VIDEO_VP9_PROFILE_0 },
6828c2ecf20Sopenharmony_ci	{ HFI_VP9_PROFILE_P2_10B, V4L2_MPEG_VIDEO_VP9_PROFILE_2 },
6838c2ecf20Sopenharmony_ci};
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_cistatic const struct id_mapping vp9_levels[] = {
6868c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_1, V4L2_MPEG_VIDEO_VP9_LEVEL_1_0 },
6878c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_11, V4L2_MPEG_VIDEO_VP9_LEVEL_1_1 },
6888c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_2, V4L2_MPEG_VIDEO_VP9_LEVEL_2_0},
6898c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_21, V4L2_MPEG_VIDEO_VP9_LEVEL_2_1 },
6908c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_3, V4L2_MPEG_VIDEO_VP9_LEVEL_3_0},
6918c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_31, V4L2_MPEG_VIDEO_VP9_LEVEL_3_1 },
6928c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_4, V4L2_MPEG_VIDEO_VP9_LEVEL_4_0 },
6938c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_41, V4L2_MPEG_VIDEO_VP9_LEVEL_4_1 },
6948c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_5, V4L2_MPEG_VIDEO_VP9_LEVEL_5_0 },
6958c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_51, V4L2_MPEG_VIDEO_VP9_LEVEL_5_1 },
6968c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_6, V4L2_MPEG_VIDEO_VP9_LEVEL_6_0 },
6978c2ecf20Sopenharmony_ci	{ HFI_VP9_LEVEL_61, V4L2_MPEG_VIDEO_VP9_LEVEL_6_1 },
6988c2ecf20Sopenharmony_ci};
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_cistatic u32 find_v4l2_id(u32 hfi_id, const struct id_mapping *array, unsigned int array_sz)
7018c2ecf20Sopenharmony_ci{
7028c2ecf20Sopenharmony_ci	unsigned int i;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	if (!array || !array_sz)
7058c2ecf20Sopenharmony_ci		return 0;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	for (i = 0; i < array_sz; i++)
7088c2ecf20Sopenharmony_ci		if (hfi_id == array[i].hfi_id)
7098c2ecf20Sopenharmony_ci			return array[i].v4l2_id;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	return 0;
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic u32 find_hfi_id(u32 v4l2_id, const struct id_mapping *array, unsigned int array_sz)
7158c2ecf20Sopenharmony_ci{
7168c2ecf20Sopenharmony_ci	unsigned int i;
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	if (!array || !array_sz)
7198c2ecf20Sopenharmony_ci		return 0;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	for (i = 0; i < array_sz; i++)
7228c2ecf20Sopenharmony_ci		if (v4l2_id == array[i].v4l2_id)
7238c2ecf20Sopenharmony_ci			return array[i].hfi_id;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	return 0;
7268c2ecf20Sopenharmony_ci}
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_cistatic void
7298c2ecf20Sopenharmony_civ4l2_id_profile_level(u32 hfi_codec, struct hfi_profile_level *pl, u32 *profile, u32 *level)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	u32 hfi_pf = pl->profile;
7328c2ecf20Sopenharmony_ci	u32 hfi_lvl = pl->level;
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	switch (hfi_codec) {
7358c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_H264:
7368c2ecf20Sopenharmony_ci		*profile = find_v4l2_id(hfi_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
7378c2ecf20Sopenharmony_ci		*level = find_v4l2_id(hfi_lvl, h264_levels, ARRAY_SIZE(h264_levels));
7388c2ecf20Sopenharmony_ci		break;
7398c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_MPEG2:
7408c2ecf20Sopenharmony_ci		*profile = find_v4l2_id(hfi_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
7418c2ecf20Sopenharmony_ci		*level = find_v4l2_id(hfi_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
7428c2ecf20Sopenharmony_ci		break;
7438c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_MPEG4:
7448c2ecf20Sopenharmony_ci		*profile = find_v4l2_id(hfi_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
7458c2ecf20Sopenharmony_ci		*level = find_v4l2_id(hfi_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
7468c2ecf20Sopenharmony_ci		break;
7478c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_VP8:
7488c2ecf20Sopenharmony_ci		*profile = find_v4l2_id(hfi_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
7498c2ecf20Sopenharmony_ci		*level = 0;
7508c2ecf20Sopenharmony_ci		break;
7518c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_VP9:
7528c2ecf20Sopenharmony_ci		*profile = find_v4l2_id(hfi_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
7538c2ecf20Sopenharmony_ci		*level = find_v4l2_id(hfi_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
7548c2ecf20Sopenharmony_ci		break;
7558c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_HEVC:
7568c2ecf20Sopenharmony_ci		*profile = find_v4l2_id(hfi_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
7578c2ecf20Sopenharmony_ci		*level = find_v4l2_id(hfi_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
7588c2ecf20Sopenharmony_ci		break;
7598c2ecf20Sopenharmony_ci	default:
7608c2ecf20Sopenharmony_ci		break;
7618c2ecf20Sopenharmony_ci	}
7628c2ecf20Sopenharmony_ci}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_cistatic void
7658c2ecf20Sopenharmony_cihfi_id_profile_level(u32 hfi_codec, u32 v4l2_pf, u32 v4l2_lvl, struct hfi_profile_level *pl)
7668c2ecf20Sopenharmony_ci{
7678c2ecf20Sopenharmony_ci	switch (hfi_codec) {
7688c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_H264:
7698c2ecf20Sopenharmony_ci		pl->profile = find_hfi_id(v4l2_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
7708c2ecf20Sopenharmony_ci		pl->level = find_hfi_id(v4l2_lvl, h264_levels, ARRAY_SIZE(h264_levels));
7718c2ecf20Sopenharmony_ci		break;
7728c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_MPEG2:
7738c2ecf20Sopenharmony_ci		pl->profile = find_hfi_id(v4l2_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
7748c2ecf20Sopenharmony_ci		pl->level = find_hfi_id(v4l2_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
7758c2ecf20Sopenharmony_ci		break;
7768c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_MPEG4:
7778c2ecf20Sopenharmony_ci		pl->profile = find_hfi_id(v4l2_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
7788c2ecf20Sopenharmony_ci		pl->level = find_hfi_id(v4l2_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
7798c2ecf20Sopenharmony_ci		break;
7808c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_VP8:
7818c2ecf20Sopenharmony_ci		pl->profile = find_hfi_id(v4l2_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
7828c2ecf20Sopenharmony_ci		pl->level = 0;
7838c2ecf20Sopenharmony_ci		break;
7848c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_VP9:
7858c2ecf20Sopenharmony_ci		pl->profile = find_hfi_id(v4l2_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
7868c2ecf20Sopenharmony_ci		pl->level = find_hfi_id(v4l2_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
7878c2ecf20Sopenharmony_ci		break;
7888c2ecf20Sopenharmony_ci	case HFI_VIDEO_CODEC_HEVC:
7898c2ecf20Sopenharmony_ci		pl->profile = find_hfi_id(v4l2_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
7908c2ecf20Sopenharmony_ci		pl->level = find_hfi_id(v4l2_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
7918c2ecf20Sopenharmony_ci		break;
7928c2ecf20Sopenharmony_ci	default:
7938c2ecf20Sopenharmony_ci		break;
7948c2ecf20Sopenharmony_ci	}
7958c2ecf20Sopenharmony_ci}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ciint venus_helper_get_profile_level(struct venus_inst *inst, u32 *profile, u32 *level)
7988c2ecf20Sopenharmony_ci{
7998c2ecf20Sopenharmony_ci	const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
8008c2ecf20Sopenharmony_ci	union hfi_get_property hprop;
8018c2ecf20Sopenharmony_ci	int ret;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	ret = hfi_session_get_property(inst, ptype, &hprop);
8048c2ecf20Sopenharmony_ci	if (ret)
8058c2ecf20Sopenharmony_ci		return ret;
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	v4l2_id_profile_level(inst->hfi_codec, &hprop.profile_level, profile, level);
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	return 0;
8108c2ecf20Sopenharmony_ci}
8118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_get_profile_level);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ciint venus_helper_set_profile_level(struct venus_inst *inst, u32 profile, u32 level)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
8168c2ecf20Sopenharmony_ci	struct hfi_profile_level pl;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	hfi_id_profile_level(inst->hfi_codec, profile, level, &pl);
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	return hfi_session_set_property(inst, ptype, &pl);
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_profile_level);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_cistatic u32 get_framesize_raw_nv12(u32 width, u32 height)
8258c2ecf20Sopenharmony_ci{
8268c2ecf20Sopenharmony_ci	u32 y_stride, uv_stride, y_plane;
8278c2ecf20Sopenharmony_ci	u32 y_sclines, uv_sclines, uv_plane;
8288c2ecf20Sopenharmony_ci	u32 size;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	y_stride = ALIGN(width, 128);
8318c2ecf20Sopenharmony_ci	uv_stride = ALIGN(width, 128);
8328c2ecf20Sopenharmony_ci	y_sclines = ALIGN(height, 32);
8338c2ecf20Sopenharmony_ci	uv_sclines = ALIGN(((height + 1) >> 1), 16);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	y_plane = y_stride * y_sclines;
8368c2ecf20Sopenharmony_ci	uv_plane = uv_stride * uv_sclines + SZ_4K;
8378c2ecf20Sopenharmony_ci	size = y_plane + uv_plane + SZ_8K;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	return ALIGN(size, SZ_4K);
8408c2ecf20Sopenharmony_ci}
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_cistatic u32 get_framesize_raw_nv12_ubwc(u32 width, u32 height)
8438c2ecf20Sopenharmony_ci{
8448c2ecf20Sopenharmony_ci	u32 y_meta_stride, y_meta_plane;
8458c2ecf20Sopenharmony_ci	u32 y_stride, y_plane;
8468c2ecf20Sopenharmony_ci	u32 uv_meta_stride, uv_meta_plane;
8478c2ecf20Sopenharmony_ci	u32 uv_stride, uv_plane;
8488c2ecf20Sopenharmony_ci	u32 extradata = SZ_16K;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
8518c2ecf20Sopenharmony_ci	y_meta_plane = y_meta_stride * ALIGN(DIV_ROUND_UP(height, 8), 16);
8528c2ecf20Sopenharmony_ci	y_meta_plane = ALIGN(y_meta_plane, SZ_4K);
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	y_stride = ALIGN(width, 128);
8558c2ecf20Sopenharmony_ci	y_plane = ALIGN(y_stride * ALIGN(height, 32), SZ_4K);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	uv_meta_stride = ALIGN(DIV_ROUND_UP(width / 2, 16), 64);
8588c2ecf20Sopenharmony_ci	uv_meta_plane = uv_meta_stride * ALIGN(DIV_ROUND_UP(height / 2, 8), 16);
8598c2ecf20Sopenharmony_ci	uv_meta_plane = ALIGN(uv_meta_plane, SZ_4K);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	uv_stride = ALIGN(width, 128);
8628c2ecf20Sopenharmony_ci	uv_plane = ALIGN(uv_stride * ALIGN(height / 2, 32), SZ_4K);
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	return ALIGN(y_meta_plane + y_plane + uv_meta_plane + uv_plane +
8658c2ecf20Sopenharmony_ci		     max(extradata, y_stride * 48), SZ_4K);
8668c2ecf20Sopenharmony_ci}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_cistatic u32 get_framesize_raw_p010(u32 width, u32 height)
8698c2ecf20Sopenharmony_ci{
8708c2ecf20Sopenharmony_ci	u32 y_plane, uv_plane, y_stride, uv_stride, y_sclines, uv_sclines;
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	y_stride = ALIGN(width * 2, 256);
8738c2ecf20Sopenharmony_ci	uv_stride = ALIGN(width * 2, 256);
8748c2ecf20Sopenharmony_ci	y_sclines = ALIGN(height, 32);
8758c2ecf20Sopenharmony_ci	uv_sclines = ALIGN((height + 1) >> 1, 16);
8768c2ecf20Sopenharmony_ci	y_plane = y_stride * y_sclines;
8778c2ecf20Sopenharmony_ci	uv_plane = uv_stride * uv_sclines;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	return ALIGN((y_plane + uv_plane), SZ_4K);
8808c2ecf20Sopenharmony_ci}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_cistatic u32 get_framesize_raw_p010_ubwc(u32 width, u32 height)
8838c2ecf20Sopenharmony_ci{
8848c2ecf20Sopenharmony_ci	u32 y_stride, uv_stride, y_sclines, uv_sclines;
8858c2ecf20Sopenharmony_ci	u32 y_ubwc_plane, uv_ubwc_plane;
8868c2ecf20Sopenharmony_ci	u32 y_meta_stride, y_meta_scanlines;
8878c2ecf20Sopenharmony_ci	u32 uv_meta_stride, uv_meta_scanlines;
8888c2ecf20Sopenharmony_ci	u32 y_meta_plane, uv_meta_plane;
8898c2ecf20Sopenharmony_ci	u32 size;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	y_stride = ALIGN(width * 2, 256);
8928c2ecf20Sopenharmony_ci	uv_stride = ALIGN(width * 2, 256);
8938c2ecf20Sopenharmony_ci	y_sclines = ALIGN(height, 16);
8948c2ecf20Sopenharmony_ci	uv_sclines = ALIGN((height + 1) >> 1, 16);
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
8978c2ecf20Sopenharmony_ci	uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
8988c2ecf20Sopenharmony_ci	y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
8998c2ecf20Sopenharmony_ci	y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
9008c2ecf20Sopenharmony_ci	y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
9018c2ecf20Sopenharmony_ci	uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 16), 64);
9028c2ecf20Sopenharmony_ci	uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
9038c2ecf20Sopenharmony_ci	uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	return ALIGN(size, SZ_4K);
9088c2ecf20Sopenharmony_ci}
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_cistatic u32 get_framesize_raw_yuv420_tp10_ubwc(u32 width, u32 height)
9118c2ecf20Sopenharmony_ci{
9128c2ecf20Sopenharmony_ci	u32 y_stride, uv_stride, y_sclines, uv_sclines;
9138c2ecf20Sopenharmony_ci	u32 y_ubwc_plane, uv_ubwc_plane;
9148c2ecf20Sopenharmony_ci	u32 y_meta_stride, y_meta_scanlines;
9158c2ecf20Sopenharmony_ci	u32 uv_meta_stride, uv_meta_scanlines;
9168c2ecf20Sopenharmony_ci	u32 y_meta_plane, uv_meta_plane;
9178c2ecf20Sopenharmony_ci	u32 extradata = SZ_16K;
9188c2ecf20Sopenharmony_ci	u32 size;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	y_stride = ALIGN(width * 4 / 3, 256);
9218c2ecf20Sopenharmony_ci	uv_stride = ALIGN(width * 4 / 3, 256);
9228c2ecf20Sopenharmony_ci	y_sclines = ALIGN(height, 16);
9238c2ecf20Sopenharmony_ci	uv_sclines = ALIGN((height + 1) >> 1, 16);
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
9268c2ecf20Sopenharmony_ci	uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
9278c2ecf20Sopenharmony_ci	y_meta_stride = ALIGN(DIV_ROUND_UP(width, 48), 64);
9288c2ecf20Sopenharmony_ci	y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
9298c2ecf20Sopenharmony_ci	y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
9308c2ecf20Sopenharmony_ci	uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 24), 64);
9318c2ecf20Sopenharmony_ci	uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
9328c2ecf20Sopenharmony_ci	uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
9358c2ecf20Sopenharmony_ci	size += max(extradata + SZ_8K, y_stride * 48);
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	return ALIGN(size, SZ_4K);
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ciu32 venus_helper_get_framesz_raw(u32 hfi_fmt, u32 width, u32 height)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	switch (hfi_fmt) {
9438c2ecf20Sopenharmony_ci	case HFI_COLOR_FORMAT_NV12:
9448c2ecf20Sopenharmony_ci	case HFI_COLOR_FORMAT_NV21:
9458c2ecf20Sopenharmony_ci		return get_framesize_raw_nv12(width, height);
9468c2ecf20Sopenharmony_ci	case HFI_COLOR_FORMAT_NV12_UBWC:
9478c2ecf20Sopenharmony_ci		return get_framesize_raw_nv12_ubwc(width, height);
9488c2ecf20Sopenharmony_ci	case HFI_COLOR_FORMAT_P010:
9498c2ecf20Sopenharmony_ci		return get_framesize_raw_p010(width, height);
9508c2ecf20Sopenharmony_ci	case HFI_COLOR_FORMAT_P010_UBWC:
9518c2ecf20Sopenharmony_ci		return get_framesize_raw_p010_ubwc(width, height);
9528c2ecf20Sopenharmony_ci	case HFI_COLOR_FORMAT_YUV420_TP10_UBWC:
9538c2ecf20Sopenharmony_ci		return get_framesize_raw_yuv420_tp10_ubwc(width, height);
9548c2ecf20Sopenharmony_ci	default:
9558c2ecf20Sopenharmony_ci		return 0;
9568c2ecf20Sopenharmony_ci	}
9578c2ecf20Sopenharmony_ci}
9588c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_get_framesz_raw);
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ciu32 venus_helper_get_framesz(u32 v4l2_fmt, u32 width, u32 height)
9618c2ecf20Sopenharmony_ci{
9628c2ecf20Sopenharmony_ci	u32 hfi_fmt, sz;
9638c2ecf20Sopenharmony_ci	bool compressed;
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	switch (v4l2_fmt) {
9668c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_MPEG:
9678c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_H264:
9688c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_H264_NO_SC:
9698c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_H264_MVC:
9708c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_H263:
9718c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_MPEG1:
9728c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_MPEG2:
9738c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_MPEG4:
9748c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_XVID:
9758c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VC1_ANNEX_G:
9768c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VC1_ANNEX_L:
9778c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VP8:
9788c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VP9:
9798c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_HEVC:
9808c2ecf20Sopenharmony_ci		compressed = true;
9818c2ecf20Sopenharmony_ci		break;
9828c2ecf20Sopenharmony_ci	default:
9838c2ecf20Sopenharmony_ci		compressed = false;
9848c2ecf20Sopenharmony_ci		break;
9858c2ecf20Sopenharmony_ci	}
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	if (compressed) {
9888c2ecf20Sopenharmony_ci		sz = ALIGN(height, 32) * ALIGN(width, 32) * 3 / 2 / 2;
9898c2ecf20Sopenharmony_ci		return ALIGN(sz, SZ_4K);
9908c2ecf20Sopenharmony_ci	}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	hfi_fmt = to_hfi_raw_fmt(v4l2_fmt);
9938c2ecf20Sopenharmony_ci	if (!hfi_fmt)
9948c2ecf20Sopenharmony_ci		return 0;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	return venus_helper_get_framesz_raw(hfi_fmt, width, height);
9978c2ecf20Sopenharmony_ci}
9988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_get_framesz);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ciint venus_helper_set_input_resolution(struct venus_inst *inst,
10018c2ecf20Sopenharmony_ci				      unsigned int width, unsigned int height)
10028c2ecf20Sopenharmony_ci{
10038c2ecf20Sopenharmony_ci	u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
10048c2ecf20Sopenharmony_ci	struct hfi_framesize fs;
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	fs.buffer_type = HFI_BUFFER_INPUT;
10078c2ecf20Sopenharmony_ci	fs.width = width;
10088c2ecf20Sopenharmony_ci	fs.height = height;
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	return hfi_session_set_property(inst, ptype, &fs);
10118c2ecf20Sopenharmony_ci}
10128c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_input_resolution);
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ciint venus_helper_set_output_resolution(struct venus_inst *inst,
10158c2ecf20Sopenharmony_ci				       unsigned int width, unsigned int height,
10168c2ecf20Sopenharmony_ci				       u32 buftype)
10178c2ecf20Sopenharmony_ci{
10188c2ecf20Sopenharmony_ci	u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
10198c2ecf20Sopenharmony_ci	struct hfi_framesize fs;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	fs.buffer_type = buftype;
10228c2ecf20Sopenharmony_ci	fs.width = width;
10238c2ecf20Sopenharmony_ci	fs.height = height;
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	return hfi_session_set_property(inst, ptype, &fs);
10268c2ecf20Sopenharmony_ci}
10278c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_output_resolution);
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ciint venus_helper_set_work_mode(struct venus_inst *inst, u32 mode)
10308c2ecf20Sopenharmony_ci{
10318c2ecf20Sopenharmony_ci	const u32 ptype = HFI_PROPERTY_PARAM_WORK_MODE;
10328c2ecf20Sopenharmony_ci	struct hfi_video_work_mode wm;
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	if (!IS_V4(inst->core))
10358c2ecf20Sopenharmony_ci		return 0;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	wm.video_work_mode = mode;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	return hfi_session_set_property(inst, ptype, &wm);
10408c2ecf20Sopenharmony_ci}
10418c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_work_mode);
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ciint venus_helper_init_codec_freq_data(struct venus_inst *inst)
10448c2ecf20Sopenharmony_ci{
10458c2ecf20Sopenharmony_ci	const struct codec_freq_data *data;
10468c2ecf20Sopenharmony_ci	unsigned int i, data_size;
10478c2ecf20Sopenharmony_ci	u32 pixfmt;
10488c2ecf20Sopenharmony_ci	int ret = 0;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	if (!IS_V4(inst->core))
10518c2ecf20Sopenharmony_ci		return 0;
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	data = inst->core->res->codec_freq_data;
10548c2ecf20Sopenharmony_ci	data_size = inst->core->res->codec_freq_data_size;
10558c2ecf20Sopenharmony_ci	pixfmt = inst->session_type == VIDC_SESSION_TYPE_DEC ?
10568c2ecf20Sopenharmony_ci			inst->fmt_out->pixfmt : inst->fmt_cap->pixfmt;
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	for (i = 0; i < data_size; i++) {
10598c2ecf20Sopenharmony_ci		if (data[i].pixfmt == pixfmt &&
10608c2ecf20Sopenharmony_ci		    data[i].session_type == inst->session_type) {
10618c2ecf20Sopenharmony_ci			inst->clk_data.codec_freq_data = &data[i];
10628c2ecf20Sopenharmony_ci			break;
10638c2ecf20Sopenharmony_ci		}
10648c2ecf20Sopenharmony_ci	}
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	if (!inst->clk_data.codec_freq_data)
10678c2ecf20Sopenharmony_ci		ret = -EINVAL;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	return ret;
10708c2ecf20Sopenharmony_ci}
10718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_init_codec_freq_data);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ciint venus_helper_set_num_bufs(struct venus_inst *inst, unsigned int input_bufs,
10748c2ecf20Sopenharmony_ci			      unsigned int output_bufs,
10758c2ecf20Sopenharmony_ci			      unsigned int output2_bufs)
10768c2ecf20Sopenharmony_ci{
10778c2ecf20Sopenharmony_ci	u32 ptype = HFI_PROPERTY_PARAM_BUFFER_COUNT_ACTUAL;
10788c2ecf20Sopenharmony_ci	struct hfi_buffer_count_actual buf_count;
10798c2ecf20Sopenharmony_ci	int ret;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	buf_count.type = HFI_BUFFER_INPUT;
10828c2ecf20Sopenharmony_ci	buf_count.count_actual = input_bufs;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	ret = hfi_session_set_property(inst, ptype, &buf_count);
10858c2ecf20Sopenharmony_ci	if (ret)
10868c2ecf20Sopenharmony_ci		return ret;
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci	buf_count.type = HFI_BUFFER_OUTPUT;
10898c2ecf20Sopenharmony_ci	buf_count.count_actual = output_bufs;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	ret = hfi_session_set_property(inst, ptype, &buf_count);
10928c2ecf20Sopenharmony_ci	if (ret)
10938c2ecf20Sopenharmony_ci		return ret;
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	if (output2_bufs) {
10968c2ecf20Sopenharmony_ci		buf_count.type = HFI_BUFFER_OUTPUT2;
10978c2ecf20Sopenharmony_ci		buf_count.count_actual = output2_bufs;
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci		ret = hfi_session_set_property(inst, ptype, &buf_count);
11008c2ecf20Sopenharmony_ci	}
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci	return ret;
11038c2ecf20Sopenharmony_ci}
11048c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_num_bufs);
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ciint venus_helper_set_raw_format(struct venus_inst *inst, u32 hfi_format,
11078c2ecf20Sopenharmony_ci				u32 buftype)
11088c2ecf20Sopenharmony_ci{
11098c2ecf20Sopenharmony_ci	const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_FORMAT_SELECT;
11108c2ecf20Sopenharmony_ci	struct hfi_uncompressed_format_select fmt;
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	fmt.buffer_type = buftype;
11138c2ecf20Sopenharmony_ci	fmt.format = hfi_format;
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	return hfi_session_set_property(inst, ptype, &fmt);
11168c2ecf20Sopenharmony_ci}
11178c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_raw_format);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ciint venus_helper_set_color_format(struct venus_inst *inst, u32 pixfmt)
11208c2ecf20Sopenharmony_ci{
11218c2ecf20Sopenharmony_ci	u32 hfi_format, buftype;
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	if (inst->session_type == VIDC_SESSION_TYPE_DEC)
11248c2ecf20Sopenharmony_ci		buftype = HFI_BUFFER_OUTPUT;
11258c2ecf20Sopenharmony_ci	else if (inst->session_type == VIDC_SESSION_TYPE_ENC)
11268c2ecf20Sopenharmony_ci		buftype = HFI_BUFFER_INPUT;
11278c2ecf20Sopenharmony_ci	else
11288c2ecf20Sopenharmony_ci		return -EINVAL;
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	hfi_format = to_hfi_raw_fmt(pixfmt);
11318c2ecf20Sopenharmony_ci	if (!hfi_format)
11328c2ecf20Sopenharmony_ci		return -EINVAL;
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	return venus_helper_set_raw_format(inst, hfi_format, buftype);
11358c2ecf20Sopenharmony_ci}
11368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_color_format);
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ciint venus_helper_set_multistream(struct venus_inst *inst, bool out_en,
11398c2ecf20Sopenharmony_ci				 bool out2_en)
11408c2ecf20Sopenharmony_ci{
11418c2ecf20Sopenharmony_ci	struct hfi_multi_stream multi = {0};
11428c2ecf20Sopenharmony_ci	u32 ptype = HFI_PROPERTY_PARAM_VDEC_MULTI_STREAM;
11438c2ecf20Sopenharmony_ci	int ret;
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	multi.buffer_type = HFI_BUFFER_OUTPUT;
11468c2ecf20Sopenharmony_ci	multi.enable = out_en;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	ret = hfi_session_set_property(inst, ptype, &multi);
11498c2ecf20Sopenharmony_ci	if (ret)
11508c2ecf20Sopenharmony_ci		return ret;
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	multi.buffer_type = HFI_BUFFER_OUTPUT2;
11538c2ecf20Sopenharmony_ci	multi.enable = out2_en;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	return hfi_session_set_property(inst, ptype, &multi);
11568c2ecf20Sopenharmony_ci}
11578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_multistream);
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ciint venus_helper_set_dyn_bufmode(struct venus_inst *inst)
11608c2ecf20Sopenharmony_ci{
11618c2ecf20Sopenharmony_ci	const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_ALLOC_MODE;
11628c2ecf20Sopenharmony_ci	struct hfi_buffer_alloc_mode mode;
11638c2ecf20Sopenharmony_ci	int ret;
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	if (!is_dynamic_bufmode(inst))
11668c2ecf20Sopenharmony_ci		return 0;
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	mode.type = HFI_BUFFER_OUTPUT;
11698c2ecf20Sopenharmony_ci	mode.mode = HFI_BUFFER_MODE_DYNAMIC;
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci	ret = hfi_session_set_property(inst, ptype, &mode);
11728c2ecf20Sopenharmony_ci	if (ret)
11738c2ecf20Sopenharmony_ci		return ret;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	mode.type = HFI_BUFFER_OUTPUT2;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	return hfi_session_set_property(inst, ptype, &mode);
11788c2ecf20Sopenharmony_ci}
11798c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_dyn_bufmode);
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ciint venus_helper_set_bufsize(struct venus_inst *inst, u32 bufsize, u32 buftype)
11828c2ecf20Sopenharmony_ci{
11838c2ecf20Sopenharmony_ci	const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_SIZE_ACTUAL;
11848c2ecf20Sopenharmony_ci	struct hfi_buffer_size_actual bufsz;
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	bufsz.type = buftype;
11878c2ecf20Sopenharmony_ci	bufsz.size = bufsize;
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	return hfi_session_set_property(inst, ptype, &bufsz);
11908c2ecf20Sopenharmony_ci}
11918c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_set_bufsize);
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ciunsigned int venus_helper_get_opb_size(struct venus_inst *inst)
11948c2ecf20Sopenharmony_ci{
11958c2ecf20Sopenharmony_ci	/* the encoder has only one output */
11968c2ecf20Sopenharmony_ci	if (inst->session_type == VIDC_SESSION_TYPE_ENC)
11978c2ecf20Sopenharmony_ci		return inst->output_buf_size;
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci	if (inst->opb_buftype == HFI_BUFFER_OUTPUT)
12008c2ecf20Sopenharmony_ci		return inst->output_buf_size;
12018c2ecf20Sopenharmony_ci	else if (inst->opb_buftype == HFI_BUFFER_OUTPUT2)
12028c2ecf20Sopenharmony_ci		return inst->output2_buf_size;
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci	return 0;
12058c2ecf20Sopenharmony_ci}
12068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_get_opb_size);
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_cistatic void delayed_process_buf_func(struct work_struct *work)
12098c2ecf20Sopenharmony_ci{
12108c2ecf20Sopenharmony_ci	struct venus_buffer *buf, *n;
12118c2ecf20Sopenharmony_ci	struct venus_inst *inst;
12128c2ecf20Sopenharmony_ci	int ret;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	inst = container_of(work, struct venus_inst, delayed_process_work);
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	mutex_lock(&inst->lock);
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	if (!(inst->streamon_out & inst->streamon_cap))
12198c2ecf20Sopenharmony_ci		goto unlock;
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	list_for_each_entry_safe(buf, n, &inst->delayed_process, ref_list) {
12228c2ecf20Sopenharmony_ci		if (buf->flags & HFI_BUFFERFLAG_READONLY)
12238c2ecf20Sopenharmony_ci			continue;
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci		ret = session_process_buf(inst, &buf->vb);
12268c2ecf20Sopenharmony_ci		if (ret)
12278c2ecf20Sopenharmony_ci			return_buf_error(inst, &buf->vb);
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci		list_del_init(&buf->ref_list);
12308c2ecf20Sopenharmony_ci	}
12318c2ecf20Sopenharmony_ciunlock:
12328c2ecf20Sopenharmony_ci	mutex_unlock(&inst->lock);
12338c2ecf20Sopenharmony_ci}
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_civoid venus_helper_release_buf_ref(struct venus_inst *inst, unsigned int idx)
12368c2ecf20Sopenharmony_ci{
12378c2ecf20Sopenharmony_ci	struct venus_buffer *buf;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
12408c2ecf20Sopenharmony_ci		if (buf->vb.vb2_buf.index == idx) {
12418c2ecf20Sopenharmony_ci			buf->flags &= ~HFI_BUFFERFLAG_READONLY;
12428c2ecf20Sopenharmony_ci			schedule_work(&inst->delayed_process_work);
12438c2ecf20Sopenharmony_ci			break;
12448c2ecf20Sopenharmony_ci		}
12458c2ecf20Sopenharmony_ci	}
12468c2ecf20Sopenharmony_ci}
12478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_release_buf_ref);
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_civoid venus_helper_acquire_buf_ref(struct vb2_v4l2_buffer *vbuf)
12508c2ecf20Sopenharmony_ci{
12518c2ecf20Sopenharmony_ci	struct venus_buffer *buf = to_venus_buffer(vbuf);
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	buf->flags |= HFI_BUFFERFLAG_READONLY;
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_acquire_buf_ref);
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_cistatic int is_buf_refed(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
12588c2ecf20Sopenharmony_ci{
12598c2ecf20Sopenharmony_ci	struct venus_buffer *buf = to_venus_buffer(vbuf);
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	if (buf->flags & HFI_BUFFERFLAG_READONLY) {
12628c2ecf20Sopenharmony_ci		list_add_tail(&buf->ref_list, &inst->delayed_process);
12638c2ecf20Sopenharmony_ci		schedule_work(&inst->delayed_process_work);
12648c2ecf20Sopenharmony_ci		return 1;
12658c2ecf20Sopenharmony_ci	}
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	return 0;
12688c2ecf20Sopenharmony_ci}
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_cistruct vb2_v4l2_buffer *
12718c2ecf20Sopenharmony_civenus_helper_find_buf(struct venus_inst *inst, unsigned int type, u32 idx)
12728c2ecf20Sopenharmony_ci{
12738c2ecf20Sopenharmony_ci	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
12768c2ecf20Sopenharmony_ci		return v4l2_m2m_src_buf_remove_by_idx(m2m_ctx, idx);
12778c2ecf20Sopenharmony_ci	else
12788c2ecf20Sopenharmony_ci		return v4l2_m2m_dst_buf_remove_by_idx(m2m_ctx, idx);
12798c2ecf20Sopenharmony_ci}
12808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_find_buf);
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ciint venus_helper_vb2_buf_init(struct vb2_buffer *vb)
12838c2ecf20Sopenharmony_ci{
12848c2ecf20Sopenharmony_ci	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
12858c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
12868c2ecf20Sopenharmony_ci	struct venus_buffer *buf = to_venus_buffer(vbuf);
12878c2ecf20Sopenharmony_ci	struct sg_table *sgt;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	sgt = vb2_dma_sg_plane_desc(vb, 0);
12908c2ecf20Sopenharmony_ci	if (!sgt)
12918c2ecf20Sopenharmony_ci		return -EFAULT;
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	buf->size = vb2_plane_size(vb, 0);
12948c2ecf20Sopenharmony_ci	buf->dma_addr = sg_dma_address(sgt->sgl);
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
12978c2ecf20Sopenharmony_ci		list_add_tail(&buf->reg_list, &inst->registeredbufs);
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	return 0;
13008c2ecf20Sopenharmony_ci}
13018c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_vb2_buf_init);
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ciint venus_helper_vb2_buf_prepare(struct vb2_buffer *vb)
13048c2ecf20Sopenharmony_ci{
13058c2ecf20Sopenharmony_ci	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
13068c2ecf20Sopenharmony_ci	unsigned int out_buf_size = venus_helper_get_opb_size(inst);
13078c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
13108c2ecf20Sopenharmony_ci		if (vbuf->field == V4L2_FIELD_ANY)
13118c2ecf20Sopenharmony_ci			vbuf->field = V4L2_FIELD_NONE;
13128c2ecf20Sopenharmony_ci		if (vbuf->field != V4L2_FIELD_NONE) {
13138c2ecf20Sopenharmony_ci			dev_err(inst->core->dev, "%s field isn't supported\n",
13148c2ecf20Sopenharmony_ci				__func__);
13158c2ecf20Sopenharmony_ci			return -EINVAL;
13168c2ecf20Sopenharmony_ci		}
13178c2ecf20Sopenharmony_ci	}
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
13208c2ecf20Sopenharmony_ci	    vb2_plane_size(vb, 0) < out_buf_size)
13218c2ecf20Sopenharmony_ci		return -EINVAL;
13228c2ecf20Sopenharmony_ci	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
13238c2ecf20Sopenharmony_ci	    vb2_plane_size(vb, 0) < inst->input_buf_size)
13248c2ecf20Sopenharmony_ci		return -EINVAL;
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	return 0;
13278c2ecf20Sopenharmony_ci}
13288c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_vb2_buf_prepare);
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_cistatic void cache_payload(struct venus_inst *inst, struct vb2_buffer *vb)
13318c2ecf20Sopenharmony_ci{
13328c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
13338c2ecf20Sopenharmony_ci	unsigned int idx = vbuf->vb2_buf.index;
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci	if (vbuf->vb2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
13368c2ecf20Sopenharmony_ci		inst->payloads[idx] = vb2_get_plane_payload(vb, 0);
13378c2ecf20Sopenharmony_ci}
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_civoid venus_helper_vb2_buf_queue(struct vb2_buffer *vb)
13408c2ecf20Sopenharmony_ci{
13418c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
13428c2ecf20Sopenharmony_ci	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
13438c2ecf20Sopenharmony_ci	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
13448c2ecf20Sopenharmony_ci	int ret;
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	mutex_lock(&inst->lock);
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	v4l2_m2m_buf_queue(m2m_ctx, vbuf);
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	/* Skip processing queued capture buffers after LAST flag */
13518c2ecf20Sopenharmony_ci	if (inst->session_type == VIDC_SESSION_TYPE_DEC &&
13528c2ecf20Sopenharmony_ci	    V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
13538c2ecf20Sopenharmony_ci	    inst->codec_state == VENUS_DEC_STATE_DRC)
13548c2ecf20Sopenharmony_ci		goto unlock;
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	cache_payload(inst, vb);
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	if (inst->session_type == VIDC_SESSION_TYPE_ENC &&
13598c2ecf20Sopenharmony_ci	    !(inst->streamon_out && inst->streamon_cap))
13608c2ecf20Sopenharmony_ci		goto unlock;
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	if (vb2_start_streaming_called(vb->vb2_queue)) {
13638c2ecf20Sopenharmony_ci		ret = is_buf_refed(inst, vbuf);
13648c2ecf20Sopenharmony_ci		if (ret)
13658c2ecf20Sopenharmony_ci			goto unlock;
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci		ret = session_process_buf(inst, vbuf);
13688c2ecf20Sopenharmony_ci		if (ret)
13698c2ecf20Sopenharmony_ci			return_buf_error(inst, vbuf);
13708c2ecf20Sopenharmony_ci	}
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ciunlock:
13738c2ecf20Sopenharmony_ci	mutex_unlock(&inst->lock);
13748c2ecf20Sopenharmony_ci}
13758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_vb2_buf_queue);
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_civoid venus_helper_buffers_done(struct venus_inst *inst, unsigned int type,
13788c2ecf20Sopenharmony_ci			       enum vb2_buffer_state state)
13798c2ecf20Sopenharmony_ci{
13808c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *buf;
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
13838c2ecf20Sopenharmony_ci		while ((buf = v4l2_m2m_src_buf_remove(inst->m2m_ctx)))
13848c2ecf20Sopenharmony_ci			v4l2_m2m_buf_done(buf, state);
13858c2ecf20Sopenharmony_ci	} else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
13868c2ecf20Sopenharmony_ci		while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
13878c2ecf20Sopenharmony_ci			v4l2_m2m_buf_done(buf, state);
13888c2ecf20Sopenharmony_ci	}
13898c2ecf20Sopenharmony_ci}
13908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_buffers_done);
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_civoid venus_helper_vb2_stop_streaming(struct vb2_queue *q)
13938c2ecf20Sopenharmony_ci{
13948c2ecf20Sopenharmony_ci	struct venus_inst *inst = vb2_get_drv_priv(q);
13958c2ecf20Sopenharmony_ci	struct venus_core *core = inst->core;
13968c2ecf20Sopenharmony_ci	int ret;
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	mutex_lock(&inst->lock);
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	if (inst->streamon_out & inst->streamon_cap) {
14018c2ecf20Sopenharmony_ci		ret = hfi_session_stop(inst);
14028c2ecf20Sopenharmony_ci		ret |= hfi_session_unload_res(inst);
14038c2ecf20Sopenharmony_ci		ret |= venus_helper_unregister_bufs(inst);
14048c2ecf20Sopenharmony_ci		ret |= venus_helper_intbufs_free(inst);
14058c2ecf20Sopenharmony_ci		ret |= hfi_session_deinit(inst);
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci		if (inst->session_error || core->sys_error)
14088c2ecf20Sopenharmony_ci			ret = -EIO;
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci		if (ret)
14118c2ecf20Sopenharmony_ci			hfi_session_abort(inst);
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci		venus_helper_free_dpb_bufs(inst);
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci		venus_pm_load_scale(inst);
14168c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&inst->registeredbufs);
14178c2ecf20Sopenharmony_ci	}
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
14208c2ecf20Sopenharmony_ci				  VB2_BUF_STATE_ERROR);
14218c2ecf20Sopenharmony_ci	venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
14228c2ecf20Sopenharmony_ci				  VB2_BUF_STATE_ERROR);
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ci	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
14258c2ecf20Sopenharmony_ci		inst->streamon_out = 0;
14268c2ecf20Sopenharmony_ci	else
14278c2ecf20Sopenharmony_ci		inst->streamon_cap = 0;
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	venus_pm_release_core(inst);
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci	mutex_unlock(&inst->lock);
14328c2ecf20Sopenharmony_ci}
14338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_vb2_stop_streaming);
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_ciint venus_helper_process_initial_cap_bufs(struct venus_inst *inst)
14368c2ecf20Sopenharmony_ci{
14378c2ecf20Sopenharmony_ci	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
14388c2ecf20Sopenharmony_ci	struct v4l2_m2m_buffer *buf, *n;
14398c2ecf20Sopenharmony_ci	int ret;
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
14428c2ecf20Sopenharmony_ci		ret = session_process_buf(inst, &buf->vb);
14438c2ecf20Sopenharmony_ci		if (ret) {
14448c2ecf20Sopenharmony_ci			return_buf_error(inst, &buf->vb);
14458c2ecf20Sopenharmony_ci			return ret;
14468c2ecf20Sopenharmony_ci		}
14478c2ecf20Sopenharmony_ci	}
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci	return 0;
14508c2ecf20Sopenharmony_ci}
14518c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_process_initial_cap_bufs);
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ciint venus_helper_process_initial_out_bufs(struct venus_inst *inst)
14548c2ecf20Sopenharmony_ci{
14558c2ecf20Sopenharmony_ci	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
14568c2ecf20Sopenharmony_ci	struct v4l2_m2m_buffer *buf, *n;
14578c2ecf20Sopenharmony_ci	int ret;
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
14608c2ecf20Sopenharmony_ci		ret = session_process_buf(inst, &buf->vb);
14618c2ecf20Sopenharmony_ci		if (ret) {
14628c2ecf20Sopenharmony_ci			return_buf_error(inst, &buf->vb);
14638c2ecf20Sopenharmony_ci			return ret;
14648c2ecf20Sopenharmony_ci		}
14658c2ecf20Sopenharmony_ci	}
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	return 0;
14688c2ecf20Sopenharmony_ci}
14698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_process_initial_out_bufs);
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ciint venus_helper_vb2_start_streaming(struct venus_inst *inst)
14728c2ecf20Sopenharmony_ci{
14738c2ecf20Sopenharmony_ci	int ret;
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	ret = venus_helper_intbufs_alloc(inst);
14768c2ecf20Sopenharmony_ci	if (ret)
14778c2ecf20Sopenharmony_ci		return ret;
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	ret = session_register_bufs(inst);
14808c2ecf20Sopenharmony_ci	if (ret)
14818c2ecf20Sopenharmony_ci		goto err_bufs_free;
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_ci	venus_pm_load_scale(inst);
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	ret = hfi_session_load_res(inst);
14868c2ecf20Sopenharmony_ci	if (ret)
14878c2ecf20Sopenharmony_ci		goto err_unreg_bufs;
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	ret = hfi_session_start(inst);
14908c2ecf20Sopenharmony_ci	if (ret)
14918c2ecf20Sopenharmony_ci		goto err_unload_res;
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci	return 0;
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_cierr_unload_res:
14968c2ecf20Sopenharmony_ci	hfi_session_unload_res(inst);
14978c2ecf20Sopenharmony_cierr_unreg_bufs:
14988c2ecf20Sopenharmony_ci	venus_helper_unregister_bufs(inst);
14998c2ecf20Sopenharmony_cierr_bufs_free:
15008c2ecf20Sopenharmony_ci	venus_helper_intbufs_free(inst);
15018c2ecf20Sopenharmony_ci	return ret;
15028c2ecf20Sopenharmony_ci}
15038c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_vb2_start_streaming);
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_civoid venus_helper_m2m_device_run(void *priv)
15068c2ecf20Sopenharmony_ci{
15078c2ecf20Sopenharmony_ci	struct venus_inst *inst = priv;
15088c2ecf20Sopenharmony_ci	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
15098c2ecf20Sopenharmony_ci	struct v4l2_m2m_buffer *buf, *n;
15108c2ecf20Sopenharmony_ci	int ret;
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	mutex_lock(&inst->lock);
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci	v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
15158c2ecf20Sopenharmony_ci		ret = session_process_buf(inst, &buf->vb);
15168c2ecf20Sopenharmony_ci		if (ret)
15178c2ecf20Sopenharmony_ci			return_buf_error(inst, &buf->vb);
15188c2ecf20Sopenharmony_ci	}
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_ci	v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
15218c2ecf20Sopenharmony_ci		ret = session_process_buf(inst, &buf->vb);
15228c2ecf20Sopenharmony_ci		if (ret)
15238c2ecf20Sopenharmony_ci			return_buf_error(inst, &buf->vb);
15248c2ecf20Sopenharmony_ci	}
15258c2ecf20Sopenharmony_ci
15268c2ecf20Sopenharmony_ci	mutex_unlock(&inst->lock);
15278c2ecf20Sopenharmony_ci}
15288c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_m2m_device_run);
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_civoid venus_helper_m2m_job_abort(void *priv)
15318c2ecf20Sopenharmony_ci{
15328c2ecf20Sopenharmony_ci	struct venus_inst *inst = priv;
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci	v4l2_m2m_job_finish(inst->m2m_dev, inst->m2m_ctx);
15358c2ecf20Sopenharmony_ci}
15368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_m2m_job_abort);
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_civoid venus_helper_init_instance(struct venus_inst *inst)
15398c2ecf20Sopenharmony_ci{
15408c2ecf20Sopenharmony_ci	if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
15418c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&inst->delayed_process);
15428c2ecf20Sopenharmony_ci		INIT_WORK(&inst->delayed_process_work,
15438c2ecf20Sopenharmony_ci			  delayed_process_buf_func);
15448c2ecf20Sopenharmony_ci	}
15458c2ecf20Sopenharmony_ci}
15468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_init_instance);
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_cistatic bool find_fmt_from_caps(struct venus_caps *caps, u32 buftype, u32 fmt)
15498c2ecf20Sopenharmony_ci{
15508c2ecf20Sopenharmony_ci	unsigned int i;
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	for (i = 0; i < caps->num_fmts; i++) {
15538c2ecf20Sopenharmony_ci		if (caps->fmts[i].buftype == buftype &&
15548c2ecf20Sopenharmony_ci		    caps->fmts[i].fmt == fmt)
15558c2ecf20Sopenharmony_ci			return true;
15568c2ecf20Sopenharmony_ci	}
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_ci	return false;
15598c2ecf20Sopenharmony_ci}
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ciint venus_helper_get_out_fmts(struct venus_inst *inst, u32 v4l2_fmt,
15628c2ecf20Sopenharmony_ci			      u32 *out_fmt, u32 *out2_fmt, bool ubwc)
15638c2ecf20Sopenharmony_ci{
15648c2ecf20Sopenharmony_ci	struct venus_core *core = inst->core;
15658c2ecf20Sopenharmony_ci	struct venus_caps *caps;
15668c2ecf20Sopenharmony_ci	u32 ubwc_fmt, fmt = to_hfi_raw_fmt(v4l2_fmt);
15678c2ecf20Sopenharmony_ci	bool found, found_ubwc;
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci	*out_fmt = *out2_fmt = 0;
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci	if (!fmt)
15728c2ecf20Sopenharmony_ci		return -EINVAL;
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
15758c2ecf20Sopenharmony_ci	if (!caps)
15768c2ecf20Sopenharmony_ci		return -EINVAL;
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci	if (inst->bit_depth == VIDC_BITDEPTH_10 &&
15798c2ecf20Sopenharmony_ci	    inst->session_type == VIDC_SESSION_TYPE_DEC) {
15808c2ecf20Sopenharmony_ci		found_ubwc =
15818c2ecf20Sopenharmony_ci			find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT,
15828c2ecf20Sopenharmony_ci					   HFI_COLOR_FORMAT_YUV420_TP10_UBWC);
15838c2ecf20Sopenharmony_ci		found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2,
15848c2ecf20Sopenharmony_ci					   HFI_COLOR_FORMAT_NV12);
15858c2ecf20Sopenharmony_ci		if (found_ubwc && found) {
15868c2ecf20Sopenharmony_ci			/*
15878c2ecf20Sopenharmony_ci			 * Hard-code DPB buffers to be 10bit UBWC and decoder
15888c2ecf20Sopenharmony_ci			 * output buffers in 8bit NV12 until V4L2 is able to
15898c2ecf20Sopenharmony_ci			 * expose compressed/tiled formats to applications.
15908c2ecf20Sopenharmony_ci			 */
15918c2ecf20Sopenharmony_ci			*out_fmt = HFI_COLOR_FORMAT_YUV420_TP10_UBWC;
15928c2ecf20Sopenharmony_ci			*out2_fmt = HFI_COLOR_FORMAT_NV12;
15938c2ecf20Sopenharmony_ci			return 0;
15948c2ecf20Sopenharmony_ci		}
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci		return -EINVAL;
15978c2ecf20Sopenharmony_ci	}
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	if (ubwc) {
16008c2ecf20Sopenharmony_ci		ubwc_fmt = fmt | HFI_COLOR_FORMAT_UBWC_BASE;
16018c2ecf20Sopenharmony_ci		found_ubwc = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT,
16028c2ecf20Sopenharmony_ci						ubwc_fmt);
16038c2ecf20Sopenharmony_ci		found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci		if (found_ubwc && found) {
16068c2ecf20Sopenharmony_ci			*out_fmt = ubwc_fmt;
16078c2ecf20Sopenharmony_ci			*out2_fmt = fmt;
16088c2ecf20Sopenharmony_ci			return 0;
16098c2ecf20Sopenharmony_ci		}
16108c2ecf20Sopenharmony_ci	}
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci	found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT, fmt);
16138c2ecf20Sopenharmony_ci	if (found) {
16148c2ecf20Sopenharmony_ci		*out_fmt = fmt;
16158c2ecf20Sopenharmony_ci		*out2_fmt = 0;
16168c2ecf20Sopenharmony_ci		return 0;
16178c2ecf20Sopenharmony_ci	}
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_ci	found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
16208c2ecf20Sopenharmony_ci	if (found) {
16218c2ecf20Sopenharmony_ci		*out_fmt = 0;
16228c2ecf20Sopenharmony_ci		*out2_fmt = fmt;
16238c2ecf20Sopenharmony_ci		return 0;
16248c2ecf20Sopenharmony_ci	}
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_ci	return -EINVAL;
16278c2ecf20Sopenharmony_ci}
16288c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(venus_helper_get_out_fmts);
1629