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