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