1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2017 Linaro Ltd.
5  */
6 #include <linux/list.h>
7 #include <linux/mutex.h>
8 #include <linux/slab.h>
9 #include <linux/kernel.h>
10 #include <media/videobuf2-dma-contig.h>
11 #include <media/v4l2-mem2mem.h>
12 #include <asm/div64.h>
13 
14 #include "core.h"
15 #include "helpers.h"
16 #include "hfi_helper.h"
17 #include "pm_helpers.h"
18 #include "hfi_platform.h"
19 #include "hfi_parser.h"
20 
21 #define NUM_MBS_720P	(((ALIGN(1280, 16)) >> 4) * ((ALIGN(736, 16)) >> 4))
22 #define NUM_MBS_4K	(((ALIGN(4096, 16)) >> 4) * ((ALIGN(2304, 16)) >> 4))
23 
24 struct intbuf {
25 	struct list_head list;
26 	u32 type;
27 	size_t size;
28 	void *va;
29 	dma_addr_t da;
30 	unsigned long attrs;
31 };
32 
33 bool venus_helper_check_codec(struct venus_inst *inst, u32 v4l2_pixfmt)
34 {
35 	struct venus_core *core = inst->core;
36 	u32 session_type = inst->session_type;
37 	u32 codec;
38 
39 	switch (v4l2_pixfmt) {
40 	case V4L2_PIX_FMT_H264:
41 		codec = HFI_VIDEO_CODEC_H264;
42 		break;
43 	case V4L2_PIX_FMT_H263:
44 		codec = HFI_VIDEO_CODEC_H263;
45 		break;
46 	case V4L2_PIX_FMT_MPEG1:
47 		codec = HFI_VIDEO_CODEC_MPEG1;
48 		break;
49 	case V4L2_PIX_FMT_MPEG2:
50 		codec = HFI_VIDEO_CODEC_MPEG2;
51 		break;
52 	case V4L2_PIX_FMT_MPEG4:
53 		codec = HFI_VIDEO_CODEC_MPEG4;
54 		break;
55 	case V4L2_PIX_FMT_VC1_ANNEX_G:
56 	case V4L2_PIX_FMT_VC1_ANNEX_L:
57 		codec = HFI_VIDEO_CODEC_VC1;
58 		break;
59 	case V4L2_PIX_FMT_VP8:
60 		codec = HFI_VIDEO_CODEC_VP8;
61 		break;
62 	case V4L2_PIX_FMT_VP9:
63 		codec = HFI_VIDEO_CODEC_VP9;
64 		break;
65 	case V4L2_PIX_FMT_XVID:
66 		codec = HFI_VIDEO_CODEC_DIVX;
67 		break;
68 	case V4L2_PIX_FMT_HEVC:
69 		codec = HFI_VIDEO_CODEC_HEVC;
70 		break;
71 	default:
72 		return false;
73 	}
74 
75 	if (session_type == VIDC_SESSION_TYPE_ENC && core->enc_codecs & codec)
76 		return true;
77 
78 	if (session_type == VIDC_SESSION_TYPE_DEC && core->dec_codecs & codec)
79 		return true;
80 
81 	return false;
82 }
83 EXPORT_SYMBOL_GPL(venus_helper_check_codec);
84 
85 int venus_helper_queue_dpb_bufs(struct venus_inst *inst)
86 {
87 	struct intbuf *buf;
88 	int ret = 0;
89 
90 	list_for_each_entry(buf, &inst->dpbbufs, list) {
91 		struct hfi_frame_data fdata;
92 
93 		memset(&fdata, 0, sizeof(fdata));
94 		fdata.alloc_len = buf->size;
95 		fdata.device_addr = buf->da;
96 		fdata.buffer_type = buf->type;
97 
98 		ret = hfi_session_process_buf(inst, &fdata);
99 		if (ret)
100 			goto fail;
101 	}
102 
103 fail:
104 	return ret;
105 }
106 EXPORT_SYMBOL_GPL(venus_helper_queue_dpb_bufs);
107 
108 int venus_helper_free_dpb_bufs(struct venus_inst *inst)
109 {
110 	struct intbuf *buf, *n;
111 
112 	list_for_each_entry_safe(buf, n, &inst->dpbbufs, list) {
113 		list_del_init(&buf->list);
114 		dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
115 			       buf->attrs);
116 		kfree(buf);
117 	}
118 
119 	INIT_LIST_HEAD(&inst->dpbbufs);
120 
121 	return 0;
122 }
123 EXPORT_SYMBOL_GPL(venus_helper_free_dpb_bufs);
124 
125 int venus_helper_alloc_dpb_bufs(struct venus_inst *inst)
126 {
127 	struct venus_core *core = inst->core;
128 	struct device *dev = core->dev;
129 	enum hfi_version ver = core->res->hfi_version;
130 	struct hfi_buffer_requirements bufreq;
131 	u32 buftype = inst->dpb_buftype;
132 	unsigned int dpb_size = 0;
133 	struct intbuf *buf;
134 	unsigned int i;
135 	u32 count;
136 	int ret;
137 
138 	/* no need to allocate dpb buffers */
139 	if (!inst->dpb_fmt)
140 		return 0;
141 
142 	if (inst->dpb_buftype == HFI_BUFFER_OUTPUT)
143 		dpb_size = inst->output_buf_size;
144 	else if (inst->dpb_buftype == HFI_BUFFER_OUTPUT2)
145 		dpb_size = inst->output2_buf_size;
146 
147 	if (!dpb_size)
148 		return 0;
149 
150 	ret = venus_helper_get_bufreq(inst, buftype, &bufreq);
151 	if (ret)
152 		return ret;
153 
154 	count = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
155 
156 	for (i = 0; i < count; i++) {
157 		buf = kzalloc(sizeof(*buf), GFP_KERNEL);
158 		if (!buf) {
159 			ret = -ENOMEM;
160 			goto fail;
161 		}
162 
163 		buf->type = buftype;
164 		buf->size = dpb_size;
165 		buf->attrs = DMA_ATTR_WRITE_COMBINE |
166 			     DMA_ATTR_NO_KERNEL_MAPPING;
167 		buf->va = dma_alloc_attrs(dev, buf->size, &buf->da, GFP_KERNEL,
168 					  buf->attrs);
169 		if (!buf->va) {
170 			kfree(buf);
171 			ret = -ENOMEM;
172 			goto fail;
173 		}
174 
175 		list_add_tail(&buf->list, &inst->dpbbufs);
176 	}
177 
178 	return 0;
179 
180 fail:
181 	venus_helper_free_dpb_bufs(inst);
182 	return ret;
183 }
184 EXPORT_SYMBOL_GPL(venus_helper_alloc_dpb_bufs);
185 
186 static int intbufs_set_buffer(struct venus_inst *inst, u32 type)
187 {
188 	struct venus_core *core = inst->core;
189 	struct device *dev = core->dev;
190 	struct hfi_buffer_requirements bufreq;
191 	struct hfi_buffer_desc bd;
192 	struct intbuf *buf;
193 	unsigned int i;
194 	int ret;
195 
196 	ret = venus_helper_get_bufreq(inst, type, &bufreq);
197 	if (ret)
198 		return 0;
199 
200 	if (!bufreq.size)
201 		return 0;
202 
203 	for (i = 0; i < bufreq.count_actual; i++) {
204 		buf = kzalloc(sizeof(*buf), GFP_KERNEL);
205 		if (!buf) {
206 			ret = -ENOMEM;
207 			goto fail;
208 		}
209 
210 		buf->type = bufreq.type;
211 		buf->size = bufreq.size;
212 		buf->attrs = DMA_ATTR_WRITE_COMBINE |
213 			     DMA_ATTR_NO_KERNEL_MAPPING;
214 		buf->va = dma_alloc_attrs(dev, buf->size, &buf->da, GFP_KERNEL,
215 					  buf->attrs);
216 		if (!buf->va) {
217 			ret = -ENOMEM;
218 			goto fail;
219 		}
220 
221 		memset(&bd, 0, sizeof(bd));
222 		bd.buffer_size = buf->size;
223 		bd.buffer_type = buf->type;
224 		bd.num_buffers = 1;
225 		bd.device_addr = buf->da;
226 
227 		ret = hfi_session_set_buffers(inst, &bd);
228 		if (ret) {
229 			dev_err(dev, "set session buffers failed\n");
230 			goto dma_free;
231 		}
232 
233 		list_add_tail(&buf->list, &inst->internalbufs);
234 	}
235 
236 	return 0;
237 
238 dma_free:
239 	dma_free_attrs(dev, buf->size, buf->va, buf->da, buf->attrs);
240 fail:
241 	kfree(buf);
242 	return ret;
243 }
244 
245 static int intbufs_unset_buffers(struct venus_inst *inst)
246 {
247 	struct hfi_buffer_desc bd = {0};
248 	struct intbuf *buf, *n;
249 	int ret = 0;
250 
251 	list_for_each_entry_safe(buf, n, &inst->internalbufs, list) {
252 		bd.buffer_size = buf->size;
253 		bd.buffer_type = buf->type;
254 		bd.num_buffers = 1;
255 		bd.device_addr = buf->da;
256 		bd.response_required = true;
257 
258 		ret = hfi_session_unset_buffers(inst, &bd);
259 
260 		list_del_init(&buf->list);
261 		dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
262 			       buf->attrs);
263 		kfree(buf);
264 	}
265 
266 	return ret;
267 }
268 
269 static const unsigned int intbuf_types_1xx[] = {
270 	HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_1XX),
271 	HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_1XX),
272 	HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_1XX),
273 	HFI_BUFFER_INTERNAL_PERSIST,
274 	HFI_BUFFER_INTERNAL_PERSIST_1,
275 };
276 
277 static const unsigned int intbuf_types_4xx[] = {
278 	HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_4XX),
279 	HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_4XX),
280 	HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_4XX),
281 	HFI_BUFFER_INTERNAL_PERSIST,
282 	HFI_BUFFER_INTERNAL_PERSIST_1,
283 };
284 
285 static const unsigned int intbuf_types_6xx[] = {
286 	HFI_BUFFER_INTERNAL_SCRATCH(HFI_VERSION_6XX),
287 	HFI_BUFFER_INTERNAL_SCRATCH_1(HFI_VERSION_6XX),
288 	HFI_BUFFER_INTERNAL_SCRATCH_2(HFI_VERSION_6XX),
289 	HFI_BUFFER_INTERNAL_PERSIST,
290 	HFI_BUFFER_INTERNAL_PERSIST_1,
291 };
292 
293 int venus_helper_intbufs_alloc(struct venus_inst *inst)
294 {
295 	const unsigned int *intbuf;
296 	size_t arr_sz, i;
297 	int ret;
298 
299 	if (IS_V6(inst->core)) {
300 		arr_sz = ARRAY_SIZE(intbuf_types_6xx);
301 		intbuf = intbuf_types_6xx;
302 	} else if (IS_V4(inst->core)) {
303 		arr_sz = ARRAY_SIZE(intbuf_types_4xx);
304 		intbuf = intbuf_types_4xx;
305 	} else {
306 		arr_sz = ARRAY_SIZE(intbuf_types_1xx);
307 		intbuf = intbuf_types_1xx;
308 	}
309 
310 	for (i = 0; i < arr_sz; i++) {
311 		ret = intbufs_set_buffer(inst, intbuf[i]);
312 		if (ret)
313 			goto error;
314 	}
315 
316 	return 0;
317 
318 error:
319 	intbufs_unset_buffers(inst);
320 	return ret;
321 }
322 EXPORT_SYMBOL_GPL(venus_helper_intbufs_alloc);
323 
324 int venus_helper_intbufs_free(struct venus_inst *inst)
325 {
326 	return intbufs_unset_buffers(inst);
327 }
328 EXPORT_SYMBOL_GPL(venus_helper_intbufs_free);
329 
330 int venus_helper_intbufs_realloc(struct venus_inst *inst)
331 {
332 	enum hfi_version ver = inst->core->res->hfi_version;
333 	struct hfi_buffer_desc bd;
334 	struct intbuf *buf, *n;
335 	int ret;
336 
337 	list_for_each_entry_safe(buf, n, &inst->internalbufs, list) {
338 		if (buf->type == HFI_BUFFER_INTERNAL_PERSIST ||
339 		    buf->type == HFI_BUFFER_INTERNAL_PERSIST_1)
340 			continue;
341 
342 		memset(&bd, 0, sizeof(bd));
343 		bd.buffer_size = buf->size;
344 		bd.buffer_type = buf->type;
345 		bd.num_buffers = 1;
346 		bd.device_addr = buf->da;
347 		bd.response_required = true;
348 
349 		ret = hfi_session_unset_buffers(inst, &bd);
350 
351 		dma_free_attrs(inst->core->dev, buf->size, buf->va, buf->da,
352 			       buf->attrs);
353 
354 		list_del_init(&buf->list);
355 		kfree(buf);
356 	}
357 
358 	ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH(ver));
359 	if (ret)
360 		goto err;
361 
362 	ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH_1(ver));
363 	if (ret)
364 		goto err;
365 
366 	ret = intbufs_set_buffer(inst, HFI_BUFFER_INTERNAL_SCRATCH_2(ver));
367 	if (ret)
368 		goto err;
369 
370 	return 0;
371 err:
372 	return ret;
373 }
374 EXPORT_SYMBOL_GPL(venus_helper_intbufs_realloc);
375 
376 static void fill_buffer_desc(const struct venus_buffer *buf,
377 			     struct hfi_buffer_desc *bd, bool response)
378 {
379 	memset(bd, 0, sizeof(*bd));
380 	bd->buffer_type = HFI_BUFFER_OUTPUT;
381 	bd->buffer_size = buf->size;
382 	bd->num_buffers = 1;
383 	bd->device_addr = buf->dma_addr;
384 	bd->response_required = response;
385 }
386 
387 static void return_buf_error(struct venus_inst *inst,
388 			     struct vb2_v4l2_buffer *vbuf)
389 {
390 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
391 
392 	if (vbuf->vb2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
393 		v4l2_m2m_src_buf_remove_by_buf(m2m_ctx, vbuf);
394 	else
395 		v4l2_m2m_dst_buf_remove_by_buf(m2m_ctx, vbuf);
396 
397 	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
398 }
399 
400 static void
401 put_ts_metadata(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
402 {
403 	struct vb2_buffer *vb = &vbuf->vb2_buf;
404 	unsigned int i;
405 	int slot = -1;
406 	u64 ts_us = vb->timestamp;
407 
408 	for (i = 0; i < ARRAY_SIZE(inst->tss); i++) {
409 		if (!inst->tss[i].used) {
410 			slot = i;
411 			break;
412 		}
413 	}
414 
415 	if (slot == -1) {
416 		dev_dbg(inst->core->dev, VDBGL "no free slot\n");
417 		return;
418 	}
419 
420 	do_div(ts_us, NSEC_PER_USEC);
421 
422 	inst->tss[slot].used = true;
423 	inst->tss[slot].flags = vbuf->flags;
424 	inst->tss[slot].tc = vbuf->timecode;
425 	inst->tss[slot].ts_us = ts_us;
426 	inst->tss[slot].ts_ns = vb->timestamp;
427 }
428 
429 void venus_helper_get_ts_metadata(struct venus_inst *inst, u64 timestamp_us,
430 				  struct vb2_v4l2_buffer *vbuf)
431 {
432 	struct vb2_buffer *vb = &vbuf->vb2_buf;
433 	unsigned int i;
434 
435 	for (i = 0; i < ARRAY_SIZE(inst->tss); ++i) {
436 		if (!inst->tss[i].used)
437 			continue;
438 
439 		if (inst->tss[i].ts_us != timestamp_us)
440 			continue;
441 
442 		inst->tss[i].used = false;
443 		vbuf->flags |= inst->tss[i].flags;
444 		vbuf->timecode = inst->tss[i].tc;
445 		vb->timestamp = inst->tss[i].ts_ns;
446 		break;
447 	}
448 }
449 EXPORT_SYMBOL_GPL(venus_helper_get_ts_metadata);
450 
451 static int
452 session_process_buf(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
453 {
454 	struct venus_buffer *buf = to_venus_buffer(vbuf);
455 	struct vb2_buffer *vb = &vbuf->vb2_buf;
456 	unsigned int type = vb->type;
457 	struct hfi_frame_data fdata;
458 	int ret;
459 
460 	memset(&fdata, 0, sizeof(fdata));
461 	fdata.alloc_len = buf->size;
462 	fdata.device_addr = buf->dma_addr;
463 	fdata.timestamp = vb->timestamp;
464 	do_div(fdata.timestamp, NSEC_PER_USEC);
465 	fdata.flags = 0;
466 	fdata.clnt_data = vbuf->vb2_buf.index;
467 
468 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
469 		fdata.buffer_type = HFI_BUFFER_INPUT;
470 		fdata.filled_len = vb2_get_plane_payload(vb, 0);
471 		fdata.offset = vb->planes[0].data_offset;
472 
473 		if (vbuf->flags & V4L2_BUF_FLAG_LAST || !fdata.filled_len)
474 			fdata.flags |= HFI_BUFFERFLAG_EOS;
475 
476 		if (inst->session_type == VIDC_SESSION_TYPE_DEC)
477 			put_ts_metadata(inst, vbuf);
478 
479 		venus_pm_load_scale(inst);
480 	} else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
481 		if (inst->session_type == VIDC_SESSION_TYPE_ENC)
482 			fdata.buffer_type = HFI_BUFFER_OUTPUT;
483 		else
484 			fdata.buffer_type = inst->opb_buftype;
485 		fdata.filled_len = 0;
486 		fdata.offset = 0;
487 	}
488 
489 	ret = hfi_session_process_buf(inst, &fdata);
490 	if (ret)
491 		return ret;
492 
493 	return 0;
494 }
495 
496 static bool is_dynamic_bufmode(struct venus_inst *inst)
497 {
498 	struct venus_core *core = inst->core;
499 	struct hfi_plat_caps *caps;
500 
501 	/*
502 	 * v4 doesn't send BUFFER_ALLOC_MODE_SUPPORTED property and supports
503 	 * dynamic buffer mode by default for HFI_BUFFER_OUTPUT/OUTPUT2.
504 	 */
505 	if (IS_V4(core) || IS_V6(core))
506 		return true;
507 
508 	caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
509 	if (!caps)
510 		return false;
511 
512 	return caps->cap_bufs_mode_dynamic;
513 }
514 
515 int venus_helper_unregister_bufs(struct venus_inst *inst)
516 {
517 	struct venus_buffer *buf, *n;
518 	struct hfi_buffer_desc bd;
519 	int ret = 0;
520 
521 	if (is_dynamic_bufmode(inst))
522 		return 0;
523 
524 	list_for_each_entry_safe(buf, n, &inst->registeredbufs, reg_list) {
525 		fill_buffer_desc(buf, &bd, true);
526 		ret = hfi_session_unset_buffers(inst, &bd);
527 		list_del_init(&buf->reg_list);
528 	}
529 
530 	return ret;
531 }
532 EXPORT_SYMBOL_GPL(venus_helper_unregister_bufs);
533 
534 static int session_register_bufs(struct venus_inst *inst)
535 {
536 	struct venus_core *core = inst->core;
537 	struct device *dev = core->dev;
538 	struct hfi_buffer_desc bd;
539 	struct venus_buffer *buf;
540 	int ret = 0;
541 
542 	if (is_dynamic_bufmode(inst))
543 		return 0;
544 
545 	list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
546 		fill_buffer_desc(buf, &bd, false);
547 		ret = hfi_session_set_buffers(inst, &bd);
548 		if (ret) {
549 			dev_err(dev, "%s: set buffer failed\n", __func__);
550 			break;
551 		}
552 	}
553 
554 	return ret;
555 }
556 
557 static u32 to_hfi_raw_fmt(u32 v4l2_fmt)
558 {
559 	switch (v4l2_fmt) {
560 	case V4L2_PIX_FMT_NV12:
561 		return HFI_COLOR_FORMAT_NV12;
562 	case V4L2_PIX_FMT_NV21:
563 		return HFI_COLOR_FORMAT_NV21;
564 	default:
565 		break;
566 	}
567 
568 	return 0;
569 }
570 
571 static int platform_get_bufreq(struct venus_inst *inst, u32 buftype,
572 			       struct hfi_buffer_requirements *req)
573 {
574 	enum hfi_version version = inst->core->res->hfi_version;
575 	const struct hfi_platform *hfi_plat;
576 	struct hfi_plat_buffers_params params;
577 	bool is_dec = inst->session_type == VIDC_SESSION_TYPE_DEC;
578 	struct venc_controls *enc_ctr = &inst->controls.enc;
579 
580 	hfi_plat = hfi_platform_get(version);
581 
582 	if (!hfi_plat || !hfi_plat->bufreq)
583 		return -EINVAL;
584 
585 	params.version = version;
586 	params.num_vpp_pipes = inst->core->res->num_vpp_pipes;
587 
588 	if (is_dec) {
589 		params.width = inst->width;
590 		params.height = inst->height;
591 		params.codec = inst->fmt_out->pixfmt;
592 		params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_cap->pixfmt);
593 		params.dec.max_mbs_per_frame = mbs_per_frame_max(inst);
594 		params.dec.buffer_size_limit = 0;
595 		params.dec.is_secondary_output =
596 			inst->opb_buftype == HFI_BUFFER_OUTPUT2;
597 		params.dec.is_interlaced =
598 			inst->pic_struct != HFI_INTERLACE_FRAME_PROGRESSIVE;
599 	} else {
600 		params.width = inst->out_width;
601 		params.height = inst->out_height;
602 		params.codec = inst->fmt_cap->pixfmt;
603 		params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_out->pixfmt);
604 		params.enc.work_mode = VIDC_WORK_MODE_2;
605 		params.enc.rc_type = HFI_RATE_CONTROL_OFF;
606 		if (enc_ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
607 			params.enc.rc_type = HFI_RATE_CONTROL_CQ;
608 		params.enc.num_b_frames = enc_ctr->num_b_frames;
609 		params.enc.is_tenbit = inst->bit_depth == VIDC_BITDEPTH_10;
610 	}
611 
612 	return hfi_plat->bufreq(&params, inst->session_type, buftype, req);
613 }
614 
615 int venus_helper_get_bufreq(struct venus_inst *inst, u32 type,
616 			    struct hfi_buffer_requirements *req)
617 {
618 	u32 ptype = HFI_PROPERTY_CONFIG_BUFFER_REQUIREMENTS;
619 	union hfi_get_property hprop;
620 	unsigned int i;
621 	int ret;
622 
623 	if (req)
624 		memset(req, 0, sizeof(*req));
625 
626 	if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
627 		req->count_min = inst->fw_min_cnt;
628 
629 	ret = platform_get_bufreq(inst, type, req);
630 	if (!ret) {
631 		if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
632 			inst->fw_min_cnt = req->count_min;
633 		return 0;
634 	}
635 
636 	ret = hfi_session_get_property(inst, ptype, &hprop);
637 	if (ret)
638 		return ret;
639 
640 	ret = -EINVAL;
641 
642 	for (i = 0; i < HFI_BUFFER_TYPE_MAX; i++) {
643 		if (hprop.bufreq[i].type != type)
644 			continue;
645 
646 		if (req)
647 			memcpy(req, &hprop.bufreq[i], sizeof(*req));
648 		ret = 0;
649 		break;
650 	}
651 
652 	return ret;
653 }
654 EXPORT_SYMBOL_GPL(venus_helper_get_bufreq);
655 
656 struct id_mapping {
657 	u32 hfi_id;
658 	u32 v4l2_id;
659 };
660 
661 static const struct id_mapping mpeg4_profiles[] = {
662 	{ HFI_MPEG4_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE },
663 	{ HFI_MPEG4_PROFILE_ADVANCEDSIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE },
664 };
665 
666 static const struct id_mapping mpeg4_levels[] = {
667 	{ HFI_MPEG4_LEVEL_0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 },
668 	{ HFI_MPEG4_LEVEL_0b, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B },
669 	{ HFI_MPEG4_LEVEL_1, V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 },
670 	{ HFI_MPEG4_LEVEL_2, V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 },
671 	{ HFI_MPEG4_LEVEL_3, V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 },
672 	{ HFI_MPEG4_LEVEL_4, V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 },
673 	{ HFI_MPEG4_LEVEL_5, V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 },
674 };
675 
676 static const struct id_mapping mpeg2_profiles[] = {
677 	{ HFI_MPEG2_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SIMPLE },
678 	{ HFI_MPEG2_PROFILE_MAIN, V4L2_MPEG_VIDEO_MPEG2_PROFILE_MAIN },
679 	{ HFI_MPEG2_PROFILE_SNR, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SNR_SCALABLE },
680 	{ HFI_MPEG2_PROFILE_SPATIAL, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SPATIALLY_SCALABLE },
681 	{ HFI_MPEG2_PROFILE_HIGH, V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH },
682 };
683 
684 static const struct id_mapping mpeg2_levels[] = {
685 	{ HFI_MPEG2_LEVEL_LL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_LOW },
686 	{ HFI_MPEG2_LEVEL_ML, V4L2_MPEG_VIDEO_MPEG2_LEVEL_MAIN },
687 	{ HFI_MPEG2_LEVEL_H14, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH_1440 },
688 	{ HFI_MPEG2_LEVEL_HL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH },
689 };
690 
691 static const struct id_mapping h264_profiles[] = {
692 	{ HFI_H264_PROFILE_BASELINE, V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE },
693 	{ HFI_H264_PROFILE_MAIN, V4L2_MPEG_VIDEO_H264_PROFILE_MAIN },
694 	{ HFI_H264_PROFILE_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH },
695 	{ HFI_H264_PROFILE_STEREO_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH },
696 	{ HFI_H264_PROFILE_MULTIVIEW_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH },
697 	{ HFI_H264_PROFILE_CONSTRAINED_BASE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE },
698 	{ HFI_H264_PROFILE_CONSTRAINED_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH },
699 };
700 
701 static const struct id_mapping h264_levels[] = {
702 	{ HFI_H264_LEVEL_1, V4L2_MPEG_VIDEO_H264_LEVEL_1_0 },
703 	{ HFI_H264_LEVEL_1b, V4L2_MPEG_VIDEO_H264_LEVEL_1B },
704 	{ HFI_H264_LEVEL_11, V4L2_MPEG_VIDEO_H264_LEVEL_1_1 },
705 	{ HFI_H264_LEVEL_12, V4L2_MPEG_VIDEO_H264_LEVEL_1_2 },
706 	{ HFI_H264_LEVEL_13, V4L2_MPEG_VIDEO_H264_LEVEL_1_3 },
707 	{ HFI_H264_LEVEL_2, V4L2_MPEG_VIDEO_H264_LEVEL_2_0 },
708 	{ HFI_H264_LEVEL_21, V4L2_MPEG_VIDEO_H264_LEVEL_2_1 },
709 	{ HFI_H264_LEVEL_22, V4L2_MPEG_VIDEO_H264_LEVEL_2_2 },
710 	{ HFI_H264_LEVEL_3, V4L2_MPEG_VIDEO_H264_LEVEL_3_0 },
711 	{ HFI_H264_LEVEL_31, V4L2_MPEG_VIDEO_H264_LEVEL_3_1 },
712 	{ HFI_H264_LEVEL_32, V4L2_MPEG_VIDEO_H264_LEVEL_3_2 },
713 	{ HFI_H264_LEVEL_4, V4L2_MPEG_VIDEO_H264_LEVEL_4_0 },
714 	{ HFI_H264_LEVEL_41, V4L2_MPEG_VIDEO_H264_LEVEL_4_1 },
715 	{ HFI_H264_LEVEL_42, V4L2_MPEG_VIDEO_H264_LEVEL_4_2 },
716 	{ HFI_H264_LEVEL_5, V4L2_MPEG_VIDEO_H264_LEVEL_5_0 },
717 	{ HFI_H264_LEVEL_51, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
718 	{ HFI_H264_LEVEL_52, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
719 };
720 
721 static const struct id_mapping hevc_profiles[] = {
722 	{ HFI_HEVC_PROFILE_MAIN, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN },
723 	{ HFI_HEVC_PROFILE_MAIN_STILL_PIC, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE },
724 	{ HFI_HEVC_PROFILE_MAIN10, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10 },
725 };
726 
727 static const struct id_mapping hevc_levels[] = {
728 	{ HFI_HEVC_LEVEL_1, V4L2_MPEG_VIDEO_HEVC_LEVEL_1 },
729 	{ HFI_HEVC_LEVEL_2, V4L2_MPEG_VIDEO_HEVC_LEVEL_2 },
730 	{ HFI_HEVC_LEVEL_21, V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1 },
731 	{ HFI_HEVC_LEVEL_3, V4L2_MPEG_VIDEO_HEVC_LEVEL_3 },
732 	{ HFI_HEVC_LEVEL_31, V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1 },
733 	{ HFI_HEVC_LEVEL_4, V4L2_MPEG_VIDEO_HEVC_LEVEL_4 },
734 	{ HFI_HEVC_LEVEL_41, V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1 },
735 	{ HFI_HEVC_LEVEL_5, V4L2_MPEG_VIDEO_HEVC_LEVEL_5 },
736 	{ HFI_HEVC_LEVEL_51, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1 },
737 	{ HFI_HEVC_LEVEL_52, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2 },
738 	{ HFI_HEVC_LEVEL_6, V4L2_MPEG_VIDEO_HEVC_LEVEL_6 },
739 	{ HFI_HEVC_LEVEL_61, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1 },
740 	{ HFI_HEVC_LEVEL_62, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2 },
741 };
742 
743 static const struct id_mapping vp8_profiles[] = {
744 	{ HFI_VPX_PROFILE_VERSION_0, V4L2_MPEG_VIDEO_VP8_PROFILE_0 },
745 	{ HFI_VPX_PROFILE_VERSION_1, V4L2_MPEG_VIDEO_VP8_PROFILE_1 },
746 	{ HFI_VPX_PROFILE_VERSION_2, V4L2_MPEG_VIDEO_VP8_PROFILE_2 },
747 	{ HFI_VPX_PROFILE_VERSION_3, V4L2_MPEG_VIDEO_VP8_PROFILE_3 },
748 };
749 
750 static const struct id_mapping vp9_profiles[] = {
751 	{ HFI_VP9_PROFILE_P0, V4L2_MPEG_VIDEO_VP9_PROFILE_0 },
752 	{ HFI_VP9_PROFILE_P2_10B, V4L2_MPEG_VIDEO_VP9_PROFILE_2 },
753 };
754 
755 static const struct id_mapping vp9_levels[] = {
756 	{ HFI_VP9_LEVEL_1, V4L2_MPEG_VIDEO_VP9_LEVEL_1_0 },
757 	{ HFI_VP9_LEVEL_11, V4L2_MPEG_VIDEO_VP9_LEVEL_1_1 },
758 	{ HFI_VP9_LEVEL_2, V4L2_MPEG_VIDEO_VP9_LEVEL_2_0},
759 	{ HFI_VP9_LEVEL_21, V4L2_MPEG_VIDEO_VP9_LEVEL_2_1 },
760 	{ HFI_VP9_LEVEL_3, V4L2_MPEG_VIDEO_VP9_LEVEL_3_0},
761 	{ HFI_VP9_LEVEL_31, V4L2_MPEG_VIDEO_VP9_LEVEL_3_1 },
762 	{ HFI_VP9_LEVEL_4, V4L2_MPEG_VIDEO_VP9_LEVEL_4_0 },
763 	{ HFI_VP9_LEVEL_41, V4L2_MPEG_VIDEO_VP9_LEVEL_4_1 },
764 	{ HFI_VP9_LEVEL_5, V4L2_MPEG_VIDEO_VP9_LEVEL_5_0 },
765 	{ HFI_VP9_LEVEL_51, V4L2_MPEG_VIDEO_VP9_LEVEL_5_1 },
766 	{ HFI_VP9_LEVEL_6, V4L2_MPEG_VIDEO_VP9_LEVEL_6_0 },
767 	{ HFI_VP9_LEVEL_61, V4L2_MPEG_VIDEO_VP9_LEVEL_6_1 },
768 };
769 
770 static u32 find_v4l2_id(u32 hfi_id, const struct id_mapping *array, unsigned int array_sz)
771 {
772 	unsigned int i;
773 
774 	if (!array || !array_sz)
775 		return 0;
776 
777 	for (i = 0; i < array_sz; i++)
778 		if (hfi_id == array[i].hfi_id)
779 			return array[i].v4l2_id;
780 
781 	return 0;
782 }
783 
784 static u32 find_hfi_id(u32 v4l2_id, const struct id_mapping *array, unsigned int array_sz)
785 {
786 	unsigned int i;
787 
788 	if (!array || !array_sz)
789 		return 0;
790 
791 	for (i = 0; i < array_sz; i++)
792 		if (v4l2_id == array[i].v4l2_id)
793 			return array[i].hfi_id;
794 
795 	return 0;
796 }
797 
798 static void
799 v4l2_id_profile_level(u32 hfi_codec, struct hfi_profile_level *pl, u32 *profile, u32 *level)
800 {
801 	u32 hfi_pf = pl->profile;
802 	u32 hfi_lvl = pl->level;
803 
804 	switch (hfi_codec) {
805 	case HFI_VIDEO_CODEC_H264:
806 		*profile = find_v4l2_id(hfi_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
807 		*level = find_v4l2_id(hfi_lvl, h264_levels, ARRAY_SIZE(h264_levels));
808 		break;
809 	case HFI_VIDEO_CODEC_MPEG2:
810 		*profile = find_v4l2_id(hfi_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
811 		*level = find_v4l2_id(hfi_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
812 		break;
813 	case HFI_VIDEO_CODEC_MPEG4:
814 		*profile = find_v4l2_id(hfi_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
815 		*level = find_v4l2_id(hfi_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
816 		break;
817 	case HFI_VIDEO_CODEC_VP8:
818 		*profile = find_v4l2_id(hfi_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
819 		*level = 0;
820 		break;
821 	case HFI_VIDEO_CODEC_VP9:
822 		*profile = find_v4l2_id(hfi_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
823 		*level = find_v4l2_id(hfi_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
824 		break;
825 	case HFI_VIDEO_CODEC_HEVC:
826 		*profile = find_v4l2_id(hfi_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
827 		*level = find_v4l2_id(hfi_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
828 		break;
829 	default:
830 		break;
831 	}
832 }
833 
834 static void
835 hfi_id_profile_level(u32 hfi_codec, u32 v4l2_pf, u32 v4l2_lvl, struct hfi_profile_level *pl)
836 {
837 	switch (hfi_codec) {
838 	case HFI_VIDEO_CODEC_H264:
839 		pl->profile = find_hfi_id(v4l2_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
840 		pl->level = find_hfi_id(v4l2_lvl, h264_levels, ARRAY_SIZE(h264_levels));
841 		break;
842 	case HFI_VIDEO_CODEC_MPEG2:
843 		pl->profile = find_hfi_id(v4l2_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
844 		pl->level = find_hfi_id(v4l2_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
845 		break;
846 	case HFI_VIDEO_CODEC_MPEG4:
847 		pl->profile = find_hfi_id(v4l2_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
848 		pl->level = find_hfi_id(v4l2_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
849 		break;
850 	case HFI_VIDEO_CODEC_VP8:
851 		pl->profile = find_hfi_id(v4l2_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
852 		pl->level = 0;
853 		break;
854 	case HFI_VIDEO_CODEC_VP9:
855 		pl->profile = find_hfi_id(v4l2_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
856 		pl->level = find_hfi_id(v4l2_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
857 		break;
858 	case HFI_VIDEO_CODEC_HEVC:
859 		pl->profile = find_hfi_id(v4l2_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
860 		pl->level = find_hfi_id(v4l2_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
861 		break;
862 	default:
863 		break;
864 	}
865 }
866 
867 int venus_helper_get_profile_level(struct venus_inst *inst, u32 *profile, u32 *level)
868 {
869 	const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
870 	union hfi_get_property hprop;
871 	int ret;
872 
873 	ret = hfi_session_get_property(inst, ptype, &hprop);
874 	if (ret)
875 		return ret;
876 
877 	v4l2_id_profile_level(inst->hfi_codec, &hprop.profile_level, profile, level);
878 
879 	return 0;
880 }
881 EXPORT_SYMBOL_GPL(venus_helper_get_profile_level);
882 
883 int venus_helper_set_profile_level(struct venus_inst *inst, u32 profile, u32 level)
884 {
885 	const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
886 	struct hfi_profile_level pl;
887 
888 	hfi_id_profile_level(inst->hfi_codec, profile, level, &pl);
889 
890 	return hfi_session_set_property(inst, ptype, &pl);
891 }
892 EXPORT_SYMBOL_GPL(venus_helper_set_profile_level);
893 
894 static u32 get_framesize_raw_nv12(u32 width, u32 height)
895 {
896 	u32 y_stride, uv_stride, y_plane;
897 	u32 y_sclines, uv_sclines, uv_plane;
898 	u32 size;
899 
900 	y_stride = ALIGN(width, 128);
901 	uv_stride = ALIGN(width, 128);
902 	y_sclines = ALIGN(height, 32);
903 	uv_sclines = ALIGN(((height + 1) >> 1), 16);
904 
905 	y_plane = y_stride * y_sclines;
906 	uv_plane = uv_stride * uv_sclines + SZ_4K;
907 	size = y_plane + uv_plane + SZ_8K;
908 
909 	return ALIGN(size, SZ_4K);
910 }
911 
912 static u32 get_framesize_raw_nv12_ubwc(u32 width, u32 height)
913 {
914 	u32 y_meta_stride, y_meta_plane;
915 	u32 y_stride, y_plane;
916 	u32 uv_meta_stride, uv_meta_plane;
917 	u32 uv_stride, uv_plane;
918 	u32 extradata = SZ_16K;
919 
920 	y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
921 	y_meta_plane = y_meta_stride * ALIGN(DIV_ROUND_UP(height, 8), 16);
922 	y_meta_plane = ALIGN(y_meta_plane, SZ_4K);
923 
924 	y_stride = ALIGN(width, 128);
925 	y_plane = ALIGN(y_stride * ALIGN(height, 32), SZ_4K);
926 
927 	uv_meta_stride = ALIGN(DIV_ROUND_UP(width / 2, 16), 64);
928 	uv_meta_plane = uv_meta_stride * ALIGN(DIV_ROUND_UP(height / 2, 8), 16);
929 	uv_meta_plane = ALIGN(uv_meta_plane, SZ_4K);
930 
931 	uv_stride = ALIGN(width, 128);
932 	uv_plane = ALIGN(uv_stride * ALIGN(height / 2, 32), SZ_4K);
933 
934 	return ALIGN(y_meta_plane + y_plane + uv_meta_plane + uv_plane +
935 		     max(extradata, y_stride * 48), SZ_4K);
936 }
937 
938 static u32 get_framesize_raw_p010(u32 width, u32 height)
939 {
940 	u32 y_plane, uv_plane, y_stride, uv_stride, y_sclines, uv_sclines;
941 
942 	y_stride = ALIGN(width * 2, 256);
943 	uv_stride = ALIGN(width * 2, 256);
944 	y_sclines = ALIGN(height, 32);
945 	uv_sclines = ALIGN((height + 1) >> 1, 16);
946 	y_plane = y_stride * y_sclines;
947 	uv_plane = uv_stride * uv_sclines;
948 
949 	return ALIGN((y_plane + uv_plane), SZ_4K);
950 }
951 
952 static u32 get_framesize_raw_p010_ubwc(u32 width, u32 height)
953 {
954 	u32 y_stride, uv_stride, y_sclines, uv_sclines;
955 	u32 y_ubwc_plane, uv_ubwc_plane;
956 	u32 y_meta_stride, y_meta_scanlines;
957 	u32 uv_meta_stride, uv_meta_scanlines;
958 	u32 y_meta_plane, uv_meta_plane;
959 	u32 size;
960 
961 	y_stride = ALIGN(width * 2, 256);
962 	uv_stride = ALIGN(width * 2, 256);
963 	y_sclines = ALIGN(height, 16);
964 	uv_sclines = ALIGN((height + 1) >> 1, 16);
965 
966 	y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
967 	uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
968 	y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
969 	y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
970 	y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
971 	uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 16), 64);
972 	uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
973 	uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
974 
975 	size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
976 
977 	return ALIGN(size, SZ_4K);
978 }
979 
980 static u32 get_framesize_raw_yuv420_tp10_ubwc(u32 width, u32 height)
981 {
982 	u32 y_stride, uv_stride, y_sclines, uv_sclines;
983 	u32 y_ubwc_plane, uv_ubwc_plane;
984 	u32 y_meta_stride, y_meta_scanlines;
985 	u32 uv_meta_stride, uv_meta_scanlines;
986 	u32 y_meta_plane, uv_meta_plane;
987 	u32 extradata = SZ_16K;
988 	u32 size;
989 
990 	y_stride = ALIGN(ALIGN(width, 192) * 4 / 3, 256);
991 	uv_stride = ALIGN(ALIGN(width, 192) * 4 / 3, 256);
992 	y_sclines = ALIGN(height, 16);
993 	uv_sclines = ALIGN((height + 1) >> 1, 16);
994 
995 	y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
996 	uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
997 	y_meta_stride = ALIGN(DIV_ROUND_UP(width, 48), 64);
998 	y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
999 	y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
1000 	uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 24), 64);
1001 	uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
1002 	uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
1003 
1004 	size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
1005 	size += max(extradata + SZ_8K, y_stride * 48);
1006 
1007 	return ALIGN(size, SZ_4K);
1008 }
1009 
1010 u32 venus_helper_get_framesz_raw(u32 hfi_fmt, u32 width, u32 height)
1011 {
1012 	switch (hfi_fmt) {
1013 	case HFI_COLOR_FORMAT_NV12:
1014 	case HFI_COLOR_FORMAT_NV21:
1015 		return get_framesize_raw_nv12(width, height);
1016 	case HFI_COLOR_FORMAT_NV12_UBWC:
1017 		return get_framesize_raw_nv12_ubwc(width, height);
1018 	case HFI_COLOR_FORMAT_P010:
1019 		return get_framesize_raw_p010(width, height);
1020 	case HFI_COLOR_FORMAT_P010_UBWC:
1021 		return get_framesize_raw_p010_ubwc(width, height);
1022 	case HFI_COLOR_FORMAT_YUV420_TP10_UBWC:
1023 		return get_framesize_raw_yuv420_tp10_ubwc(width, height);
1024 	default:
1025 		return 0;
1026 	}
1027 }
1028 EXPORT_SYMBOL_GPL(venus_helper_get_framesz_raw);
1029 
1030 u32 venus_helper_get_framesz(u32 v4l2_fmt, u32 width, u32 height)
1031 {
1032 	u32 hfi_fmt, sz;
1033 	bool compressed;
1034 
1035 	switch (v4l2_fmt) {
1036 	case V4L2_PIX_FMT_MPEG:
1037 	case V4L2_PIX_FMT_H264:
1038 	case V4L2_PIX_FMT_H264_NO_SC:
1039 	case V4L2_PIX_FMT_H264_MVC:
1040 	case V4L2_PIX_FMT_H263:
1041 	case V4L2_PIX_FMT_MPEG1:
1042 	case V4L2_PIX_FMT_MPEG2:
1043 	case V4L2_PIX_FMT_MPEG4:
1044 	case V4L2_PIX_FMT_XVID:
1045 	case V4L2_PIX_FMT_VC1_ANNEX_G:
1046 	case V4L2_PIX_FMT_VC1_ANNEX_L:
1047 	case V4L2_PIX_FMT_VP8:
1048 	case V4L2_PIX_FMT_VP9:
1049 	case V4L2_PIX_FMT_HEVC:
1050 		compressed = true;
1051 		break;
1052 	default:
1053 		compressed = false;
1054 		break;
1055 	}
1056 
1057 	if (compressed) {
1058 		sz = ALIGN(height, 32) * ALIGN(width, 32) * 3 / 2 / 2;
1059 		if (width < 1280 || height < 720)
1060 			sz *= 8;
1061 		return ALIGN(sz, SZ_4K);
1062 	}
1063 
1064 	hfi_fmt = to_hfi_raw_fmt(v4l2_fmt);
1065 	if (!hfi_fmt)
1066 		return 0;
1067 
1068 	return venus_helper_get_framesz_raw(hfi_fmt, width, height);
1069 }
1070 EXPORT_SYMBOL_GPL(venus_helper_get_framesz);
1071 
1072 int venus_helper_set_input_resolution(struct venus_inst *inst,
1073 				      unsigned int width, unsigned int height)
1074 {
1075 	u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
1076 	struct hfi_framesize fs;
1077 
1078 	fs.buffer_type = HFI_BUFFER_INPUT;
1079 	fs.width = width;
1080 	fs.height = height;
1081 
1082 	return hfi_session_set_property(inst, ptype, &fs);
1083 }
1084 EXPORT_SYMBOL_GPL(venus_helper_set_input_resolution);
1085 
1086 int venus_helper_set_output_resolution(struct venus_inst *inst,
1087 				       unsigned int width, unsigned int height,
1088 				       u32 buftype)
1089 {
1090 	u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
1091 	struct hfi_framesize fs;
1092 
1093 	fs.buffer_type = buftype;
1094 	fs.width = width;
1095 	fs.height = height;
1096 
1097 	return hfi_session_set_property(inst, ptype, &fs);
1098 }
1099 EXPORT_SYMBOL_GPL(venus_helper_set_output_resolution);
1100 
1101 static u32 venus_helper_get_work_mode(struct venus_inst *inst)
1102 {
1103 	u32 mode;
1104 	u32 num_mbs;
1105 
1106 	mode = VIDC_WORK_MODE_2;
1107 	if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
1108 		num_mbs = (ALIGN(inst->height, 16) * ALIGN(inst->width, 16)) / 256;
1109 		if (inst->hfi_codec == HFI_VIDEO_CODEC_MPEG2 ||
1110 		    inst->pic_struct != HFI_INTERLACE_FRAME_PROGRESSIVE ||
1111 		    num_mbs <= NUM_MBS_720P)
1112 			mode = VIDC_WORK_MODE_1;
1113 	} else {
1114 		num_mbs = (ALIGN(inst->out_height, 16) * ALIGN(inst->out_width, 16)) / 256;
1115 		if (inst->hfi_codec == HFI_VIDEO_CODEC_VP8 &&
1116 		    num_mbs <= NUM_MBS_4K)
1117 			mode = VIDC_WORK_MODE_1;
1118 	}
1119 
1120 	return mode;
1121 }
1122 
1123 int venus_helper_set_work_mode(struct venus_inst *inst)
1124 {
1125 	const u32 ptype = HFI_PROPERTY_PARAM_WORK_MODE;
1126 	struct hfi_video_work_mode wm;
1127 	u32 mode;
1128 
1129 	if (!IS_V4(inst->core) && !IS_V6(inst->core))
1130 		return 0;
1131 
1132 	mode = venus_helper_get_work_mode(inst);
1133 	wm.video_work_mode = mode;
1134 	return hfi_session_set_property(inst, ptype, &wm);
1135 }
1136 EXPORT_SYMBOL_GPL(venus_helper_set_work_mode);
1137 
1138 int venus_helper_set_format_constraints(struct venus_inst *inst)
1139 {
1140 	const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_PLANE_ACTUAL_CONSTRAINTS_INFO;
1141 	struct hfi_uncompressed_plane_actual_constraints_info pconstraint;
1142 
1143 	if (!IS_V6(inst->core))
1144 		return 0;
1145 
1146 	if (inst->opb_fmt == HFI_COLOR_FORMAT_NV12_UBWC)
1147 		return 0;
1148 
1149 	pconstraint.buffer_type = HFI_BUFFER_OUTPUT2;
1150 	pconstraint.num_planes = 2;
1151 	pconstraint.plane_format[0].stride_multiples = 128;
1152 	pconstraint.plane_format[0].max_stride = 8192;
1153 	pconstraint.plane_format[0].min_plane_buffer_height_multiple = 32;
1154 	pconstraint.plane_format[0].buffer_alignment = 256;
1155 
1156 	pconstraint.plane_format[1].stride_multiples = 128;
1157 	pconstraint.plane_format[1].max_stride = 8192;
1158 	pconstraint.plane_format[1].min_plane_buffer_height_multiple = 16;
1159 	pconstraint.plane_format[1].buffer_alignment = 256;
1160 
1161 	return hfi_session_set_property(inst, ptype, &pconstraint);
1162 }
1163 EXPORT_SYMBOL_GPL(venus_helper_set_format_constraints);
1164 
1165 int venus_helper_set_num_bufs(struct venus_inst *inst, unsigned int input_bufs,
1166 			      unsigned int output_bufs,
1167 			      unsigned int output2_bufs)
1168 {
1169 	u32 ptype = HFI_PROPERTY_PARAM_BUFFER_COUNT_ACTUAL;
1170 	struct hfi_buffer_count_actual buf_count;
1171 	int ret;
1172 
1173 	buf_count.type = HFI_BUFFER_INPUT;
1174 	buf_count.count_actual = input_bufs;
1175 
1176 	ret = hfi_session_set_property(inst, ptype, &buf_count);
1177 	if (ret)
1178 		return ret;
1179 
1180 	buf_count.type = HFI_BUFFER_OUTPUT;
1181 	buf_count.count_actual = output_bufs;
1182 
1183 	ret = hfi_session_set_property(inst, ptype, &buf_count);
1184 	if (ret)
1185 		return ret;
1186 
1187 	if (output2_bufs) {
1188 		buf_count.type = HFI_BUFFER_OUTPUT2;
1189 		buf_count.count_actual = output2_bufs;
1190 
1191 		ret = hfi_session_set_property(inst, ptype, &buf_count);
1192 	}
1193 
1194 	return ret;
1195 }
1196 EXPORT_SYMBOL_GPL(venus_helper_set_num_bufs);
1197 
1198 int venus_helper_set_raw_format(struct venus_inst *inst, u32 hfi_format,
1199 				u32 buftype)
1200 {
1201 	const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_FORMAT_SELECT;
1202 	struct hfi_uncompressed_format_select fmt;
1203 
1204 	fmt.buffer_type = buftype;
1205 	fmt.format = hfi_format;
1206 
1207 	return hfi_session_set_property(inst, ptype, &fmt);
1208 }
1209 EXPORT_SYMBOL_GPL(venus_helper_set_raw_format);
1210 
1211 int venus_helper_set_color_format(struct venus_inst *inst, u32 pixfmt)
1212 {
1213 	u32 hfi_format, buftype;
1214 
1215 	if (inst->session_type == VIDC_SESSION_TYPE_DEC)
1216 		buftype = HFI_BUFFER_OUTPUT;
1217 	else if (inst->session_type == VIDC_SESSION_TYPE_ENC)
1218 		buftype = HFI_BUFFER_INPUT;
1219 	else
1220 		return -EINVAL;
1221 
1222 	hfi_format = to_hfi_raw_fmt(pixfmt);
1223 	if (!hfi_format)
1224 		return -EINVAL;
1225 
1226 	return venus_helper_set_raw_format(inst, hfi_format, buftype);
1227 }
1228 EXPORT_SYMBOL_GPL(venus_helper_set_color_format);
1229 
1230 int venus_helper_set_multistream(struct venus_inst *inst, bool out_en,
1231 				 bool out2_en)
1232 {
1233 	struct hfi_multi_stream multi = {0};
1234 	u32 ptype = HFI_PROPERTY_PARAM_VDEC_MULTI_STREAM;
1235 	int ret;
1236 
1237 	multi.buffer_type = HFI_BUFFER_OUTPUT;
1238 	multi.enable = out_en;
1239 
1240 	ret = hfi_session_set_property(inst, ptype, &multi);
1241 	if (ret)
1242 		return ret;
1243 
1244 	multi.buffer_type = HFI_BUFFER_OUTPUT2;
1245 	multi.enable = out2_en;
1246 
1247 	return hfi_session_set_property(inst, ptype, &multi);
1248 }
1249 EXPORT_SYMBOL_GPL(venus_helper_set_multistream);
1250 
1251 int venus_helper_set_dyn_bufmode(struct venus_inst *inst)
1252 {
1253 	const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_ALLOC_MODE;
1254 	struct hfi_buffer_alloc_mode mode;
1255 	int ret;
1256 
1257 	if (!is_dynamic_bufmode(inst))
1258 		return 0;
1259 
1260 	mode.type = HFI_BUFFER_OUTPUT;
1261 	mode.mode = HFI_BUFFER_MODE_DYNAMIC;
1262 
1263 	ret = hfi_session_set_property(inst, ptype, &mode);
1264 	if (ret)
1265 		return ret;
1266 
1267 	mode.type = HFI_BUFFER_OUTPUT2;
1268 
1269 	return hfi_session_set_property(inst, ptype, &mode);
1270 }
1271 EXPORT_SYMBOL_GPL(venus_helper_set_dyn_bufmode);
1272 
1273 int venus_helper_set_bufsize(struct venus_inst *inst, u32 bufsize, u32 buftype)
1274 {
1275 	const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_SIZE_ACTUAL;
1276 	struct hfi_buffer_size_actual bufsz;
1277 
1278 	bufsz.type = buftype;
1279 	bufsz.size = bufsize;
1280 
1281 	return hfi_session_set_property(inst, ptype, &bufsz);
1282 }
1283 EXPORT_SYMBOL_GPL(venus_helper_set_bufsize);
1284 
1285 unsigned int venus_helper_get_opb_size(struct venus_inst *inst)
1286 {
1287 	/* the encoder has only one output */
1288 	if (inst->session_type == VIDC_SESSION_TYPE_ENC)
1289 		return inst->output_buf_size;
1290 
1291 	if (inst->opb_buftype == HFI_BUFFER_OUTPUT)
1292 		return inst->output_buf_size;
1293 	else if (inst->opb_buftype == HFI_BUFFER_OUTPUT2)
1294 		return inst->output2_buf_size;
1295 
1296 	return 0;
1297 }
1298 EXPORT_SYMBOL_GPL(venus_helper_get_opb_size);
1299 
1300 static void delayed_process_buf_func(struct work_struct *work)
1301 {
1302 	struct venus_buffer *buf, *n;
1303 	struct venus_inst *inst;
1304 	int ret;
1305 
1306 	inst = container_of(work, struct venus_inst, delayed_process_work);
1307 
1308 	mutex_lock(&inst->lock);
1309 
1310 	if (!(inst->streamon_out & inst->streamon_cap))
1311 		goto unlock;
1312 
1313 	list_for_each_entry_safe(buf, n, &inst->delayed_process, ref_list) {
1314 		if (buf->flags & HFI_BUFFERFLAG_READONLY)
1315 			continue;
1316 
1317 		ret = session_process_buf(inst, &buf->vb);
1318 		if (ret)
1319 			return_buf_error(inst, &buf->vb);
1320 
1321 		list_del_init(&buf->ref_list);
1322 	}
1323 unlock:
1324 	mutex_unlock(&inst->lock);
1325 }
1326 
1327 void venus_helper_release_buf_ref(struct venus_inst *inst, unsigned int idx)
1328 {
1329 	struct venus_buffer *buf;
1330 
1331 	list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
1332 		if (buf->vb.vb2_buf.index == idx) {
1333 			buf->flags &= ~HFI_BUFFERFLAG_READONLY;
1334 			schedule_work(&inst->delayed_process_work);
1335 			break;
1336 		}
1337 	}
1338 }
1339 EXPORT_SYMBOL_GPL(venus_helper_release_buf_ref);
1340 
1341 void venus_helper_acquire_buf_ref(struct vb2_v4l2_buffer *vbuf)
1342 {
1343 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1344 
1345 	buf->flags |= HFI_BUFFERFLAG_READONLY;
1346 }
1347 EXPORT_SYMBOL_GPL(venus_helper_acquire_buf_ref);
1348 
1349 static int is_buf_refed(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
1350 {
1351 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1352 
1353 	if (buf->flags & HFI_BUFFERFLAG_READONLY) {
1354 		list_add_tail(&buf->ref_list, &inst->delayed_process);
1355 		schedule_work(&inst->delayed_process_work);
1356 		return 1;
1357 	}
1358 
1359 	return 0;
1360 }
1361 
1362 struct vb2_v4l2_buffer *
1363 venus_helper_find_buf(struct venus_inst *inst, unsigned int type, u32 idx)
1364 {
1365 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1366 
1367 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1368 		return v4l2_m2m_src_buf_remove_by_idx(m2m_ctx, idx);
1369 	else
1370 		return v4l2_m2m_dst_buf_remove_by_idx(m2m_ctx, idx);
1371 }
1372 EXPORT_SYMBOL_GPL(venus_helper_find_buf);
1373 
1374 int venus_helper_vb2_buf_init(struct vb2_buffer *vb)
1375 {
1376 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1377 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1378 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1379 
1380 	buf->size = vb2_plane_size(vb, 0);
1381 	buf->dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1382 
1383 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1384 		list_add_tail(&buf->reg_list, &inst->registeredbufs);
1385 
1386 	return 0;
1387 }
1388 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_init);
1389 
1390 int venus_helper_vb2_buf_prepare(struct vb2_buffer *vb)
1391 {
1392 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1393 	unsigned int out_buf_size = venus_helper_get_opb_size(inst);
1394 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1395 
1396 	if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1397 		if (vbuf->field == V4L2_FIELD_ANY)
1398 			vbuf->field = V4L2_FIELD_NONE;
1399 		if (vbuf->field != V4L2_FIELD_NONE) {
1400 			dev_err(inst->core->dev, "%s field isn't supported\n",
1401 				__func__);
1402 			return -EINVAL;
1403 		}
1404 	}
1405 
1406 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
1407 	    vb2_plane_size(vb, 0) < out_buf_size)
1408 		return -EINVAL;
1409 	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1410 	    vb2_plane_size(vb, 0) < inst->input_buf_size)
1411 		return -EINVAL;
1412 
1413 	return 0;
1414 }
1415 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_prepare);
1416 
1417 static void cache_payload(struct venus_inst *inst, struct vb2_buffer *vb)
1418 {
1419 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1420 	unsigned int idx = vbuf->vb2_buf.index;
1421 
1422 	if (vbuf->vb2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1423 		inst->payloads[idx] = vb2_get_plane_payload(vb, 0);
1424 }
1425 
1426 void venus_helper_vb2_buf_queue(struct vb2_buffer *vb)
1427 {
1428 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1429 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1430 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1431 	int ret;
1432 
1433 	v4l2_m2m_buf_queue(m2m_ctx, vbuf);
1434 
1435 	/* Skip processing queued capture buffers after LAST flag */
1436 	if (inst->session_type == VIDC_SESSION_TYPE_DEC &&
1437 	    V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1438 	    inst->codec_state == VENUS_DEC_STATE_DRC)
1439 		return;
1440 
1441 	cache_payload(inst, vb);
1442 
1443 	if (inst->session_type == VIDC_SESSION_TYPE_ENC &&
1444 	    !(inst->streamon_out && inst->streamon_cap))
1445 		return;
1446 
1447 	if (vb2_start_streaming_called(vb->vb2_queue)) {
1448 		ret = is_buf_refed(inst, vbuf);
1449 		if (ret)
1450 			return;
1451 
1452 		ret = session_process_buf(inst, vbuf);
1453 		if (ret)
1454 			return_buf_error(inst, vbuf);
1455 	}
1456 }
1457 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_queue);
1458 
1459 void venus_helper_buffers_done(struct venus_inst *inst, unsigned int type,
1460 			       enum vb2_buffer_state state)
1461 {
1462 	struct vb2_v4l2_buffer *buf;
1463 
1464 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1465 		while ((buf = v4l2_m2m_src_buf_remove(inst->m2m_ctx)))
1466 			v4l2_m2m_buf_done(buf, state);
1467 	} else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1468 		while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1469 			v4l2_m2m_buf_done(buf, state);
1470 	}
1471 }
1472 EXPORT_SYMBOL_GPL(venus_helper_buffers_done);
1473 
1474 void venus_helper_vb2_stop_streaming(struct vb2_queue *q)
1475 {
1476 	struct venus_inst *inst = vb2_get_drv_priv(q);
1477 	struct venus_core *core = inst->core;
1478 	int ret;
1479 
1480 	mutex_lock(&inst->lock);
1481 
1482 	if (inst->streamon_out & inst->streamon_cap) {
1483 		ret = hfi_session_stop(inst);
1484 		ret |= hfi_session_unload_res(inst);
1485 		ret |= venus_helper_unregister_bufs(inst);
1486 		ret |= venus_helper_intbufs_free(inst);
1487 		ret |= hfi_session_deinit(inst);
1488 
1489 		if (inst->session_error || core->sys_error)
1490 			ret = -EIO;
1491 
1492 		if (ret)
1493 			hfi_session_abort(inst);
1494 
1495 		venus_helper_free_dpb_bufs(inst);
1496 
1497 		venus_pm_load_scale(inst);
1498 		INIT_LIST_HEAD(&inst->registeredbufs);
1499 	}
1500 
1501 	venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
1502 				  VB2_BUF_STATE_ERROR);
1503 	venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1504 				  VB2_BUF_STATE_ERROR);
1505 
1506 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1507 		inst->streamon_out = 0;
1508 	else
1509 		inst->streamon_cap = 0;
1510 
1511 	venus_pm_release_core(inst);
1512 
1513 	mutex_unlock(&inst->lock);
1514 }
1515 EXPORT_SYMBOL_GPL(venus_helper_vb2_stop_streaming);
1516 
1517 int venus_helper_process_initial_cap_bufs(struct venus_inst *inst)
1518 {
1519 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1520 	struct v4l2_m2m_buffer *buf, *n;
1521 	int ret;
1522 
1523 	v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
1524 		ret = session_process_buf(inst, &buf->vb);
1525 		if (ret) {
1526 			return_buf_error(inst, &buf->vb);
1527 			return ret;
1528 		}
1529 	}
1530 
1531 	return 0;
1532 }
1533 EXPORT_SYMBOL_GPL(venus_helper_process_initial_cap_bufs);
1534 
1535 int venus_helper_process_initial_out_bufs(struct venus_inst *inst)
1536 {
1537 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1538 	struct v4l2_m2m_buffer *buf, *n;
1539 	int ret;
1540 
1541 	v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
1542 		ret = session_process_buf(inst, &buf->vb);
1543 		if (ret) {
1544 			return_buf_error(inst, &buf->vb);
1545 			return ret;
1546 		}
1547 	}
1548 
1549 	return 0;
1550 }
1551 EXPORT_SYMBOL_GPL(venus_helper_process_initial_out_bufs);
1552 
1553 int venus_helper_vb2_start_streaming(struct venus_inst *inst)
1554 {
1555 	int ret;
1556 
1557 	ret = venus_helper_intbufs_alloc(inst);
1558 	if (ret)
1559 		return ret;
1560 
1561 	ret = session_register_bufs(inst);
1562 	if (ret)
1563 		goto err_bufs_free;
1564 
1565 	venus_pm_load_scale(inst);
1566 
1567 	ret = hfi_session_load_res(inst);
1568 	if (ret)
1569 		goto err_unreg_bufs;
1570 
1571 	ret = hfi_session_start(inst);
1572 	if (ret)
1573 		goto err_unload_res;
1574 
1575 	return 0;
1576 
1577 err_unload_res:
1578 	hfi_session_unload_res(inst);
1579 err_unreg_bufs:
1580 	venus_helper_unregister_bufs(inst);
1581 err_bufs_free:
1582 	venus_helper_intbufs_free(inst);
1583 	return ret;
1584 }
1585 EXPORT_SYMBOL_GPL(venus_helper_vb2_start_streaming);
1586 
1587 void venus_helper_m2m_device_run(void *priv)
1588 {
1589 	struct venus_inst *inst = priv;
1590 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1591 	struct v4l2_m2m_buffer *buf, *n;
1592 	int ret;
1593 
1594 	mutex_lock(&inst->lock);
1595 
1596 	v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
1597 		ret = session_process_buf(inst, &buf->vb);
1598 		if (ret)
1599 			return_buf_error(inst, &buf->vb);
1600 	}
1601 
1602 	v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
1603 		ret = session_process_buf(inst, &buf->vb);
1604 		if (ret)
1605 			return_buf_error(inst, &buf->vb);
1606 	}
1607 
1608 	mutex_unlock(&inst->lock);
1609 }
1610 EXPORT_SYMBOL_GPL(venus_helper_m2m_device_run);
1611 
1612 void venus_helper_m2m_job_abort(void *priv)
1613 {
1614 	struct venus_inst *inst = priv;
1615 
1616 	v4l2_m2m_job_finish(inst->m2m_dev, inst->m2m_ctx);
1617 }
1618 EXPORT_SYMBOL_GPL(venus_helper_m2m_job_abort);
1619 
1620 int venus_helper_session_init(struct venus_inst *inst)
1621 {
1622 	enum hfi_version version = inst->core->res->hfi_version;
1623 	u32 session_type = inst->session_type;
1624 	u32 codec;
1625 	int ret;
1626 
1627 	codec = inst->session_type == VIDC_SESSION_TYPE_DEC ?
1628 			inst->fmt_out->pixfmt : inst->fmt_cap->pixfmt;
1629 
1630 	ret = hfi_session_init(inst, codec);
1631 	if (ret)
1632 		return ret;
1633 
1634 	inst->clk_data.vpp_freq = hfi_platform_get_codec_vpp_freq(version, codec,
1635 								  session_type);
1636 	inst->clk_data.vsp_freq = hfi_platform_get_codec_vsp_freq(version, codec,
1637 								  session_type);
1638 	inst->clk_data.low_power_freq = hfi_platform_get_codec_lp_freq(version, codec,
1639 								       session_type);
1640 
1641 	return 0;
1642 }
1643 EXPORT_SYMBOL_GPL(venus_helper_session_init);
1644 
1645 void venus_helper_init_instance(struct venus_inst *inst)
1646 {
1647 	if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
1648 		INIT_LIST_HEAD(&inst->delayed_process);
1649 		INIT_WORK(&inst->delayed_process_work,
1650 			  delayed_process_buf_func);
1651 	}
1652 }
1653 EXPORT_SYMBOL_GPL(venus_helper_init_instance);
1654 
1655 static bool find_fmt_from_caps(struct hfi_plat_caps *caps, u32 buftype, u32 fmt)
1656 {
1657 	unsigned int i;
1658 
1659 	for (i = 0; i < caps->num_fmts; i++) {
1660 		if (caps->fmts[i].buftype == buftype &&
1661 		    caps->fmts[i].fmt == fmt)
1662 			return true;
1663 	}
1664 
1665 	return false;
1666 }
1667 
1668 int venus_helper_get_out_fmts(struct venus_inst *inst, u32 v4l2_fmt,
1669 			      u32 *out_fmt, u32 *out2_fmt, bool ubwc)
1670 {
1671 	struct venus_core *core = inst->core;
1672 	struct hfi_plat_caps *caps;
1673 	u32 ubwc_fmt, fmt = to_hfi_raw_fmt(v4l2_fmt);
1674 	bool found, found_ubwc;
1675 
1676 	*out_fmt = *out2_fmt = 0;
1677 
1678 	if (!fmt)
1679 		return -EINVAL;
1680 
1681 	caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
1682 	if (!caps)
1683 		return -EINVAL;
1684 
1685 	if (inst->bit_depth == VIDC_BITDEPTH_10 &&
1686 	    inst->session_type == VIDC_SESSION_TYPE_DEC) {
1687 		found_ubwc =
1688 			find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT,
1689 					   HFI_COLOR_FORMAT_YUV420_TP10_UBWC);
1690 		found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2,
1691 					   HFI_COLOR_FORMAT_NV12);
1692 		if (found_ubwc && found) {
1693 			/*
1694 			 * Hard-code DPB buffers to be 10bit UBWC and decoder
1695 			 * output buffers in 8bit NV12 until V4L2 is able to
1696 			 * expose compressed/tiled formats to applications.
1697 			 */
1698 			*out_fmt = HFI_COLOR_FORMAT_YUV420_TP10_UBWC;
1699 			*out2_fmt = HFI_COLOR_FORMAT_NV12;
1700 			return 0;
1701 		}
1702 
1703 		return -EINVAL;
1704 	}
1705 
1706 	if (ubwc) {
1707 		ubwc_fmt = fmt | HFI_COLOR_FORMAT_UBWC_BASE;
1708 		found_ubwc = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT,
1709 						ubwc_fmt);
1710 		found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1711 
1712 		if (found_ubwc && found) {
1713 			*out_fmt = ubwc_fmt;
1714 			*out2_fmt = fmt;
1715 			return 0;
1716 		}
1717 	}
1718 
1719 	found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT, fmt);
1720 	if (found) {
1721 		*out_fmt = fmt;
1722 		*out2_fmt = 0;
1723 		return 0;
1724 	}
1725 
1726 	found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1727 	if (found) {
1728 		*out_fmt = 0;
1729 		*out2_fmt = fmt;
1730 		return 0;
1731 	}
1732 
1733 	return -EINVAL;
1734 }
1735 EXPORT_SYMBOL_GPL(venus_helper_get_out_fmts);
1736 
1737 int venus_helper_set_stride(struct venus_inst *inst,
1738 			    unsigned int width, unsigned int height)
1739 {
1740 	const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_PLANE_ACTUAL_INFO;
1741 
1742 	struct hfi_uncompressed_plane_actual_info plane_actual_info;
1743 
1744 	plane_actual_info.buffer_type = HFI_BUFFER_INPUT;
1745 	plane_actual_info.num_planes = 2;
1746 	plane_actual_info.plane_format[0].actual_stride = width;
1747 	plane_actual_info.plane_format[0].actual_plane_buffer_height = height;
1748 	plane_actual_info.plane_format[1].actual_stride = width;
1749 	plane_actual_info.plane_format[1].actual_plane_buffer_height = height / 2;
1750 
1751 	return hfi_session_set_property(inst, ptype, &plane_actual_info);
1752 }
1753 EXPORT_SYMBOL_GPL(venus_helper_set_stride);
1754