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 
venus_helper_check_codec(struct venus_inst * inst,u32 v4l2_pixfmt)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 
free_dpb_buf(struct venus_inst * inst,struct intbuf * buf)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 
venus_helper_queue_dpb_bufs(struct venus_inst * inst)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 
venus_helper_free_dpb_bufs(struct venus_inst * inst)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 
venus_helper_alloc_dpb_bufs(struct venus_inst * inst)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 
intbufs_set_buffer(struct venus_inst * inst,u32 type)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 
intbufs_unset_buffers(struct venus_inst * inst)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 
venus_helper_intbufs_alloc(struct venus_inst * inst)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 
venus_helper_intbufs_free(struct venus_inst * inst)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 
venus_helper_intbufs_realloc(struct venus_inst * inst)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 
fill_buffer_desc(const struct venus_buffer * buf,struct hfi_buffer_desc * bd,bool response)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 
return_buf_error(struct venus_inst * inst,struct vb2_v4l2_buffer * vbuf)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
put_ts_metadata(struct venus_inst * inst,struct vb2_v4l2_buffer * vbuf)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 
venus_helper_get_ts_metadata(struct venus_inst * inst,u64 timestamp_us,struct vb2_v4l2_buffer * vbuf)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
session_process_buf(struct venus_inst * inst,struct vb2_v4l2_buffer * vbuf)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 	int ret;
506 
507 	memset(&fdata, 0, sizeof(fdata));
508 	fdata.alloc_len = buf->size;
509 	fdata.device_addr = buf->dma_addr;
510 	fdata.timestamp = vb->timestamp;
511 	do_div(fdata.timestamp, NSEC_PER_USEC);
512 	fdata.flags = 0;
513 	fdata.clnt_data = vbuf->vb2_buf.index;
514 
515 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
516 		fdata.buffer_type = HFI_BUFFER_INPUT;
517 		fdata.filled_len = vb2_get_plane_payload(vb, 0);
518 		fdata.offset = vb->planes[0].data_offset;
519 
520 		if (vbuf->flags & V4L2_BUF_FLAG_LAST || !fdata.filled_len)
521 			fdata.flags |= HFI_BUFFERFLAG_EOS;
522 
523 		if (inst->session_type == VIDC_SESSION_TYPE_DEC)
524 			put_ts_metadata(inst, vbuf);
525 
526 		venus_pm_load_scale(inst);
527 	} else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
528 		if (inst->session_type == VIDC_SESSION_TYPE_ENC)
529 			fdata.buffer_type = HFI_BUFFER_OUTPUT;
530 		else
531 			fdata.buffer_type = inst->opb_buftype;
532 		fdata.filled_len = 0;
533 		fdata.offset = 0;
534 	}
535 
536 	ret = hfi_session_process_buf(inst, &fdata);
537 	if (ret)
538 		return ret;
539 
540 	return 0;
541 }
542 
is_dynamic_bufmode(struct venus_inst * inst)543 static bool is_dynamic_bufmode(struct venus_inst *inst)
544 {
545 	struct venus_core *core = inst->core;
546 	struct hfi_plat_caps *caps;
547 
548 	/*
549 	 * v4 doesn't send BUFFER_ALLOC_MODE_SUPPORTED property and supports
550 	 * dynamic buffer mode by default for HFI_BUFFER_OUTPUT/OUTPUT2.
551 	 */
552 	if (IS_V4(core) || IS_V6(core))
553 		return true;
554 
555 	caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
556 	if (!caps)
557 		return false;
558 
559 	return caps->cap_bufs_mode_dynamic;
560 }
561 
venus_helper_unregister_bufs(struct venus_inst * inst)562 int venus_helper_unregister_bufs(struct venus_inst *inst)
563 {
564 	struct venus_buffer *buf, *n;
565 	struct hfi_buffer_desc bd;
566 	int ret = 0;
567 
568 	if (is_dynamic_bufmode(inst))
569 		return 0;
570 
571 	list_for_each_entry_safe(buf, n, &inst->registeredbufs, reg_list) {
572 		fill_buffer_desc(buf, &bd, true);
573 		ret = hfi_session_unset_buffers(inst, &bd);
574 		list_del_init(&buf->reg_list);
575 	}
576 
577 	return ret;
578 }
579 EXPORT_SYMBOL_GPL(venus_helper_unregister_bufs);
580 
session_register_bufs(struct venus_inst * inst)581 static int session_register_bufs(struct venus_inst *inst)
582 {
583 	struct venus_core *core = inst->core;
584 	struct device *dev = core->dev;
585 	struct hfi_buffer_desc bd;
586 	struct venus_buffer *buf;
587 	int ret = 0;
588 
589 	if (is_dynamic_bufmode(inst))
590 		return 0;
591 
592 	list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
593 		fill_buffer_desc(buf, &bd, false);
594 		ret = hfi_session_set_buffers(inst, &bd);
595 		if (ret) {
596 			dev_err(dev, "%s: set buffer failed\n", __func__);
597 			break;
598 		}
599 	}
600 
601 	return ret;
602 }
603 
to_hfi_raw_fmt(u32 v4l2_fmt)604 static u32 to_hfi_raw_fmt(u32 v4l2_fmt)
605 {
606 	switch (v4l2_fmt) {
607 	case V4L2_PIX_FMT_NV12:
608 		return HFI_COLOR_FORMAT_NV12;
609 	case V4L2_PIX_FMT_NV21:
610 		return HFI_COLOR_FORMAT_NV21;
611 	case V4L2_PIX_FMT_QC08C:
612 		return HFI_COLOR_FORMAT_NV12_UBWC;
613 	case V4L2_PIX_FMT_QC10C:
614 		return HFI_COLOR_FORMAT_YUV420_TP10_UBWC;
615 	default:
616 		break;
617 	}
618 
619 	return 0;
620 }
621 
platform_get_bufreq(struct venus_inst * inst,u32 buftype,struct hfi_buffer_requirements * req)622 static int platform_get_bufreq(struct venus_inst *inst, u32 buftype,
623 			       struct hfi_buffer_requirements *req)
624 {
625 	enum hfi_version version = inst->core->res->hfi_version;
626 	const struct hfi_platform *hfi_plat;
627 	struct hfi_plat_buffers_params params;
628 	bool is_dec = inst->session_type == VIDC_SESSION_TYPE_DEC;
629 	struct venc_controls *enc_ctr = &inst->controls.enc;
630 
631 	hfi_plat = hfi_platform_get(version);
632 
633 	if (!hfi_plat || !hfi_plat->bufreq)
634 		return -EINVAL;
635 
636 	params.version = version;
637 	params.num_vpp_pipes = inst->core->res->num_vpp_pipes;
638 
639 	if (is_dec) {
640 		params.width = inst->width;
641 		params.height = inst->height;
642 		params.codec = inst->fmt_out->pixfmt;
643 		params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_cap->pixfmt);
644 		params.dec.max_mbs_per_frame = mbs_per_frame_max(inst);
645 		params.dec.buffer_size_limit = 0;
646 		params.dec.is_secondary_output =
647 			inst->opb_buftype == HFI_BUFFER_OUTPUT2;
648 		params.dec.is_interlaced =
649 			inst->pic_struct != HFI_INTERLACE_FRAME_PROGRESSIVE;
650 	} else {
651 		params.width = inst->out_width;
652 		params.height = inst->out_height;
653 		params.codec = inst->fmt_cap->pixfmt;
654 		params.hfi_color_fmt = to_hfi_raw_fmt(inst->fmt_out->pixfmt);
655 		params.enc.work_mode = VIDC_WORK_MODE_2;
656 		params.enc.rc_type = HFI_RATE_CONTROL_OFF;
657 		if (enc_ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
658 			params.enc.rc_type = HFI_RATE_CONTROL_CQ;
659 		params.enc.num_b_frames = enc_ctr->num_b_frames;
660 		params.enc.is_tenbit = inst->bit_depth == VIDC_BITDEPTH_10;
661 	}
662 
663 	return hfi_plat->bufreq(&params, inst->session_type, buftype, req);
664 }
665 
venus_helper_get_bufreq(struct venus_inst * inst,u32 type,struct hfi_buffer_requirements * req)666 int venus_helper_get_bufreq(struct venus_inst *inst, u32 type,
667 			    struct hfi_buffer_requirements *req)
668 {
669 	u32 ptype = HFI_PROPERTY_CONFIG_BUFFER_REQUIREMENTS;
670 	union hfi_get_property hprop;
671 	unsigned int i;
672 	int ret;
673 
674 	memset(req, 0, sizeof(*req));
675 
676 	if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
677 		req->count_min = inst->fw_min_cnt;
678 
679 	ret = platform_get_bufreq(inst, type, req);
680 	if (!ret) {
681 		if (type == HFI_BUFFER_OUTPUT || type == HFI_BUFFER_OUTPUT2)
682 			inst->fw_min_cnt = req->count_min;
683 		return 0;
684 	}
685 
686 	ret = hfi_session_get_property(inst, ptype, &hprop);
687 	if (ret)
688 		return ret;
689 
690 	ret = -EINVAL;
691 
692 	for (i = 0; i < HFI_BUFFER_TYPE_MAX; i++) {
693 		if (hprop.bufreq[i].type != type)
694 			continue;
695 
696 		memcpy(req, &hprop.bufreq[i], sizeof(*req));
697 		ret = 0;
698 		break;
699 	}
700 
701 	return ret;
702 }
703 EXPORT_SYMBOL_GPL(venus_helper_get_bufreq);
704 
705 struct id_mapping {
706 	u32 hfi_id;
707 	u32 v4l2_id;
708 };
709 
710 static const struct id_mapping mpeg4_profiles[] = {
711 	{ HFI_MPEG4_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE },
712 	{ HFI_MPEG4_PROFILE_ADVANCEDSIMPLE, V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE },
713 };
714 
715 static const struct id_mapping mpeg4_levels[] = {
716 	{ HFI_MPEG4_LEVEL_0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 },
717 	{ HFI_MPEG4_LEVEL_0b, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B },
718 	{ HFI_MPEG4_LEVEL_1, V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 },
719 	{ HFI_MPEG4_LEVEL_2, V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 },
720 	{ HFI_MPEG4_LEVEL_3, V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 },
721 	{ HFI_MPEG4_LEVEL_4, V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 },
722 	{ HFI_MPEG4_LEVEL_5, V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 },
723 };
724 
725 static const struct id_mapping mpeg2_profiles[] = {
726 	{ HFI_MPEG2_PROFILE_SIMPLE, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SIMPLE },
727 	{ HFI_MPEG2_PROFILE_MAIN, V4L2_MPEG_VIDEO_MPEG2_PROFILE_MAIN },
728 	{ HFI_MPEG2_PROFILE_SNR, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SNR_SCALABLE },
729 	{ HFI_MPEG2_PROFILE_SPATIAL, V4L2_MPEG_VIDEO_MPEG2_PROFILE_SPATIALLY_SCALABLE },
730 	{ HFI_MPEG2_PROFILE_HIGH, V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH },
731 };
732 
733 static const struct id_mapping mpeg2_levels[] = {
734 	{ HFI_MPEG2_LEVEL_LL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_LOW },
735 	{ HFI_MPEG2_LEVEL_ML, V4L2_MPEG_VIDEO_MPEG2_LEVEL_MAIN },
736 	{ HFI_MPEG2_LEVEL_H14, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH_1440 },
737 	{ HFI_MPEG2_LEVEL_HL, V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH },
738 };
739 
740 static const struct id_mapping h264_profiles[] = {
741 	{ HFI_H264_PROFILE_BASELINE, V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE },
742 	{ HFI_H264_PROFILE_MAIN, V4L2_MPEG_VIDEO_H264_PROFILE_MAIN },
743 	{ HFI_H264_PROFILE_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH },
744 	{ HFI_H264_PROFILE_STEREO_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH },
745 	{ HFI_H264_PROFILE_MULTIVIEW_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH },
746 	{ HFI_H264_PROFILE_CONSTRAINED_BASE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE },
747 	{ HFI_H264_PROFILE_CONSTRAINED_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH },
748 };
749 
750 static const struct id_mapping h264_levels[] = {
751 	{ HFI_H264_LEVEL_1, V4L2_MPEG_VIDEO_H264_LEVEL_1_0 },
752 	{ HFI_H264_LEVEL_1b, V4L2_MPEG_VIDEO_H264_LEVEL_1B },
753 	{ HFI_H264_LEVEL_11, V4L2_MPEG_VIDEO_H264_LEVEL_1_1 },
754 	{ HFI_H264_LEVEL_12, V4L2_MPEG_VIDEO_H264_LEVEL_1_2 },
755 	{ HFI_H264_LEVEL_13, V4L2_MPEG_VIDEO_H264_LEVEL_1_3 },
756 	{ HFI_H264_LEVEL_2, V4L2_MPEG_VIDEO_H264_LEVEL_2_0 },
757 	{ HFI_H264_LEVEL_21, V4L2_MPEG_VIDEO_H264_LEVEL_2_1 },
758 	{ HFI_H264_LEVEL_22, V4L2_MPEG_VIDEO_H264_LEVEL_2_2 },
759 	{ HFI_H264_LEVEL_3, V4L2_MPEG_VIDEO_H264_LEVEL_3_0 },
760 	{ HFI_H264_LEVEL_31, V4L2_MPEG_VIDEO_H264_LEVEL_3_1 },
761 	{ HFI_H264_LEVEL_32, V4L2_MPEG_VIDEO_H264_LEVEL_3_2 },
762 	{ HFI_H264_LEVEL_4, V4L2_MPEG_VIDEO_H264_LEVEL_4_0 },
763 	{ HFI_H264_LEVEL_41, V4L2_MPEG_VIDEO_H264_LEVEL_4_1 },
764 	{ HFI_H264_LEVEL_42, V4L2_MPEG_VIDEO_H264_LEVEL_4_2 },
765 	{ HFI_H264_LEVEL_5, V4L2_MPEG_VIDEO_H264_LEVEL_5_0 },
766 	{ HFI_H264_LEVEL_51, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
767 	{ HFI_H264_LEVEL_52, V4L2_MPEG_VIDEO_H264_LEVEL_5_1 },
768 };
769 
770 static const struct id_mapping hevc_profiles[] = {
771 	{ HFI_HEVC_PROFILE_MAIN, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN },
772 	{ HFI_HEVC_PROFILE_MAIN_STILL_PIC, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE },
773 	{ HFI_HEVC_PROFILE_MAIN10, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10 },
774 };
775 
776 static const struct id_mapping hevc_levels[] = {
777 	{ HFI_HEVC_LEVEL_1, V4L2_MPEG_VIDEO_HEVC_LEVEL_1 },
778 	{ HFI_HEVC_LEVEL_2, V4L2_MPEG_VIDEO_HEVC_LEVEL_2 },
779 	{ HFI_HEVC_LEVEL_21, V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1 },
780 	{ HFI_HEVC_LEVEL_3, V4L2_MPEG_VIDEO_HEVC_LEVEL_3 },
781 	{ HFI_HEVC_LEVEL_31, V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1 },
782 	{ HFI_HEVC_LEVEL_4, V4L2_MPEG_VIDEO_HEVC_LEVEL_4 },
783 	{ HFI_HEVC_LEVEL_41, V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1 },
784 	{ HFI_HEVC_LEVEL_5, V4L2_MPEG_VIDEO_HEVC_LEVEL_5 },
785 	{ HFI_HEVC_LEVEL_51, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1 },
786 	{ HFI_HEVC_LEVEL_52, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2 },
787 	{ HFI_HEVC_LEVEL_6, V4L2_MPEG_VIDEO_HEVC_LEVEL_6 },
788 	{ HFI_HEVC_LEVEL_61, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1 },
789 	{ HFI_HEVC_LEVEL_62, V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2 },
790 };
791 
792 static const struct id_mapping vp8_profiles[] = {
793 	{ HFI_VPX_PROFILE_VERSION_0, V4L2_MPEG_VIDEO_VP8_PROFILE_0 },
794 	{ HFI_VPX_PROFILE_VERSION_1, V4L2_MPEG_VIDEO_VP8_PROFILE_1 },
795 	{ HFI_VPX_PROFILE_VERSION_2, V4L2_MPEG_VIDEO_VP8_PROFILE_2 },
796 	{ HFI_VPX_PROFILE_VERSION_3, V4L2_MPEG_VIDEO_VP8_PROFILE_3 },
797 };
798 
799 static const struct id_mapping vp9_profiles[] = {
800 	{ HFI_VP9_PROFILE_P0, V4L2_MPEG_VIDEO_VP9_PROFILE_0 },
801 	{ HFI_VP9_PROFILE_P2_10B, V4L2_MPEG_VIDEO_VP9_PROFILE_2 },
802 };
803 
804 static const struct id_mapping vp9_levels[] = {
805 	{ HFI_VP9_LEVEL_1, V4L2_MPEG_VIDEO_VP9_LEVEL_1_0 },
806 	{ HFI_VP9_LEVEL_11, V4L2_MPEG_VIDEO_VP9_LEVEL_1_1 },
807 	{ HFI_VP9_LEVEL_2, V4L2_MPEG_VIDEO_VP9_LEVEL_2_0},
808 	{ HFI_VP9_LEVEL_21, V4L2_MPEG_VIDEO_VP9_LEVEL_2_1 },
809 	{ HFI_VP9_LEVEL_3, V4L2_MPEG_VIDEO_VP9_LEVEL_3_0},
810 	{ HFI_VP9_LEVEL_31, V4L2_MPEG_VIDEO_VP9_LEVEL_3_1 },
811 	{ HFI_VP9_LEVEL_4, V4L2_MPEG_VIDEO_VP9_LEVEL_4_0 },
812 	{ HFI_VP9_LEVEL_41, V4L2_MPEG_VIDEO_VP9_LEVEL_4_1 },
813 	{ HFI_VP9_LEVEL_5, V4L2_MPEG_VIDEO_VP9_LEVEL_5_0 },
814 	{ HFI_VP9_LEVEL_51, V4L2_MPEG_VIDEO_VP9_LEVEL_5_1 },
815 	{ HFI_VP9_LEVEL_6, V4L2_MPEG_VIDEO_VP9_LEVEL_6_0 },
816 	{ HFI_VP9_LEVEL_61, V4L2_MPEG_VIDEO_VP9_LEVEL_6_1 },
817 };
818 
find_v4l2_id(u32 hfi_id,const struct id_mapping * array,unsigned int array_sz)819 static u32 find_v4l2_id(u32 hfi_id, const struct id_mapping *array, unsigned int array_sz)
820 {
821 	unsigned int i;
822 
823 	if (!array || !array_sz)
824 		return 0;
825 
826 	for (i = 0; i < array_sz; i++)
827 		if (hfi_id == array[i].hfi_id)
828 			return array[i].v4l2_id;
829 
830 	return 0;
831 }
832 
find_hfi_id(u32 v4l2_id,const struct id_mapping * array,unsigned int array_sz)833 static u32 find_hfi_id(u32 v4l2_id, const struct id_mapping *array, unsigned int array_sz)
834 {
835 	unsigned int i;
836 
837 	if (!array || !array_sz)
838 		return 0;
839 
840 	for (i = 0; i < array_sz; i++)
841 		if (v4l2_id == array[i].v4l2_id)
842 			return array[i].hfi_id;
843 
844 	return 0;
845 }
846 
847 static void
v4l2_id_profile_level(u32 hfi_codec,struct hfi_profile_level * pl,u32 * profile,u32 * level)848 v4l2_id_profile_level(u32 hfi_codec, struct hfi_profile_level *pl, u32 *profile, u32 *level)
849 {
850 	u32 hfi_pf = pl->profile;
851 	u32 hfi_lvl = pl->level;
852 
853 	switch (hfi_codec) {
854 	case HFI_VIDEO_CODEC_H264:
855 		*profile = find_v4l2_id(hfi_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
856 		*level = find_v4l2_id(hfi_lvl, h264_levels, ARRAY_SIZE(h264_levels));
857 		break;
858 	case HFI_VIDEO_CODEC_MPEG2:
859 		*profile = find_v4l2_id(hfi_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
860 		*level = find_v4l2_id(hfi_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
861 		break;
862 	case HFI_VIDEO_CODEC_MPEG4:
863 		*profile = find_v4l2_id(hfi_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
864 		*level = find_v4l2_id(hfi_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
865 		break;
866 	case HFI_VIDEO_CODEC_VP8:
867 		*profile = find_v4l2_id(hfi_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
868 		*level = 0;
869 		break;
870 	case HFI_VIDEO_CODEC_VP9:
871 		*profile = find_v4l2_id(hfi_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
872 		*level = find_v4l2_id(hfi_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
873 		break;
874 	case HFI_VIDEO_CODEC_HEVC:
875 		*profile = find_v4l2_id(hfi_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
876 		*level = find_v4l2_id(hfi_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
877 		break;
878 	default:
879 		break;
880 	}
881 }
882 
883 static void
hfi_id_profile_level(u32 hfi_codec,u32 v4l2_pf,u32 v4l2_lvl,struct hfi_profile_level * pl)884 hfi_id_profile_level(u32 hfi_codec, u32 v4l2_pf, u32 v4l2_lvl, struct hfi_profile_level *pl)
885 {
886 	switch (hfi_codec) {
887 	case HFI_VIDEO_CODEC_H264:
888 		pl->profile = find_hfi_id(v4l2_pf, h264_profiles, ARRAY_SIZE(h264_profiles));
889 		pl->level = find_hfi_id(v4l2_lvl, h264_levels, ARRAY_SIZE(h264_levels));
890 		break;
891 	case HFI_VIDEO_CODEC_MPEG2:
892 		pl->profile = find_hfi_id(v4l2_pf, mpeg2_profiles, ARRAY_SIZE(mpeg2_profiles));
893 		pl->level = find_hfi_id(v4l2_lvl, mpeg2_levels, ARRAY_SIZE(mpeg2_levels));
894 		break;
895 	case HFI_VIDEO_CODEC_MPEG4:
896 		pl->profile = find_hfi_id(v4l2_pf, mpeg4_profiles, ARRAY_SIZE(mpeg4_profiles));
897 		pl->level = find_hfi_id(v4l2_lvl, mpeg4_levels, ARRAY_SIZE(mpeg4_levels));
898 		break;
899 	case HFI_VIDEO_CODEC_VP8:
900 		pl->profile = find_hfi_id(v4l2_pf, vp8_profiles, ARRAY_SIZE(vp8_profiles));
901 		pl->level = 0;
902 		break;
903 	case HFI_VIDEO_CODEC_VP9:
904 		pl->profile = find_hfi_id(v4l2_pf, vp9_profiles, ARRAY_SIZE(vp9_profiles));
905 		pl->level = find_hfi_id(v4l2_lvl, vp9_levels, ARRAY_SIZE(vp9_levels));
906 		break;
907 	case HFI_VIDEO_CODEC_HEVC:
908 		pl->profile = find_hfi_id(v4l2_pf, hevc_profiles, ARRAY_SIZE(hevc_profiles));
909 		pl->level = find_hfi_id(v4l2_lvl, hevc_levels, ARRAY_SIZE(hevc_levels));
910 		break;
911 	default:
912 		break;
913 	}
914 }
915 
venus_helper_get_profile_level(struct venus_inst * inst,u32 * profile,u32 * level)916 int venus_helper_get_profile_level(struct venus_inst *inst, u32 *profile, u32 *level)
917 {
918 	const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
919 	union hfi_get_property hprop;
920 	int ret;
921 
922 	ret = hfi_session_get_property(inst, ptype, &hprop);
923 	if (ret)
924 		return ret;
925 
926 	v4l2_id_profile_level(inst->hfi_codec, &hprop.profile_level, profile, level);
927 
928 	return 0;
929 }
930 EXPORT_SYMBOL_GPL(venus_helper_get_profile_level);
931 
venus_helper_set_profile_level(struct venus_inst * inst,u32 profile,u32 level)932 int venus_helper_set_profile_level(struct venus_inst *inst, u32 profile, u32 level)
933 {
934 	const u32 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
935 	struct hfi_profile_level pl;
936 
937 	hfi_id_profile_level(inst->hfi_codec, profile, level, &pl);
938 
939 	return hfi_session_set_property(inst, ptype, &pl);
940 }
941 EXPORT_SYMBOL_GPL(venus_helper_set_profile_level);
942 
get_framesize_raw_nv12(u32 width,u32 height)943 static u32 get_framesize_raw_nv12(u32 width, u32 height)
944 {
945 	u32 y_stride, uv_stride, y_plane;
946 	u32 y_sclines, uv_sclines, uv_plane;
947 	u32 size;
948 
949 	y_stride = ALIGN(width, 128);
950 	uv_stride = ALIGN(width, 128);
951 	y_sclines = ALIGN(height, 32);
952 	uv_sclines = ALIGN(((height + 1) >> 1), 16);
953 
954 	y_plane = y_stride * y_sclines;
955 	uv_plane = uv_stride * uv_sclines + SZ_4K;
956 	size = y_plane + uv_plane + SZ_8K;
957 
958 	return ALIGN(size, SZ_4K);
959 }
960 
get_framesize_raw_nv12_ubwc(u32 width,u32 height)961 static u32 get_framesize_raw_nv12_ubwc(u32 width, u32 height)
962 {
963 	u32 y_meta_stride, y_meta_plane;
964 	u32 y_stride, y_plane;
965 	u32 uv_meta_stride, uv_meta_plane;
966 	u32 uv_stride, uv_plane;
967 	u32 extradata = SZ_16K;
968 
969 	y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
970 	y_meta_plane = y_meta_stride * ALIGN(DIV_ROUND_UP(height, 8), 16);
971 	y_meta_plane = ALIGN(y_meta_plane, SZ_4K);
972 
973 	y_stride = ALIGN(width, 128);
974 	y_plane = ALIGN(y_stride * ALIGN(height, 32), SZ_4K);
975 
976 	uv_meta_stride = ALIGN(DIV_ROUND_UP(width / 2, 16), 64);
977 	uv_meta_plane = uv_meta_stride * ALIGN(DIV_ROUND_UP(height / 2, 8), 16);
978 	uv_meta_plane = ALIGN(uv_meta_plane, SZ_4K);
979 
980 	uv_stride = ALIGN(width, 128);
981 	uv_plane = ALIGN(uv_stride * ALIGN(height / 2, 32), SZ_4K);
982 
983 	return ALIGN(y_meta_plane + y_plane + uv_meta_plane + uv_plane +
984 		     max(extradata, y_stride * 48), SZ_4K);
985 }
986 
get_framesize_raw_p010(u32 width,u32 height)987 static u32 get_framesize_raw_p010(u32 width, u32 height)
988 {
989 	u32 y_plane, uv_plane, y_stride, uv_stride, y_sclines, uv_sclines;
990 
991 	y_stride = ALIGN(width * 2, 256);
992 	uv_stride = ALIGN(width * 2, 256);
993 	y_sclines = ALIGN(height, 32);
994 	uv_sclines = ALIGN((height + 1) >> 1, 16);
995 	y_plane = y_stride * y_sclines;
996 	uv_plane = uv_stride * uv_sclines;
997 
998 	return ALIGN((y_plane + uv_plane), SZ_4K);
999 }
1000 
get_framesize_raw_p010_ubwc(u32 width,u32 height)1001 static u32 get_framesize_raw_p010_ubwc(u32 width, u32 height)
1002 {
1003 	u32 y_stride, uv_stride, y_sclines, uv_sclines;
1004 	u32 y_ubwc_plane, uv_ubwc_plane;
1005 	u32 y_meta_stride, y_meta_scanlines;
1006 	u32 uv_meta_stride, uv_meta_scanlines;
1007 	u32 y_meta_plane, uv_meta_plane;
1008 	u32 size;
1009 
1010 	y_stride = ALIGN(width * 2, 256);
1011 	uv_stride = ALIGN(width * 2, 256);
1012 	y_sclines = ALIGN(height, 16);
1013 	uv_sclines = ALIGN((height + 1) >> 1, 16);
1014 
1015 	y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
1016 	uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
1017 	y_meta_stride = ALIGN(DIV_ROUND_UP(width, 32), 64);
1018 	y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
1019 	y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
1020 	uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 16), 64);
1021 	uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
1022 	uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
1023 
1024 	size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
1025 
1026 	return ALIGN(size, SZ_4K);
1027 }
1028 
get_framesize_raw_yuv420_tp10_ubwc(u32 width,u32 height)1029 static u32 get_framesize_raw_yuv420_tp10_ubwc(u32 width, u32 height)
1030 {
1031 	u32 y_stride, uv_stride, y_sclines, uv_sclines;
1032 	u32 y_ubwc_plane, uv_ubwc_plane;
1033 	u32 y_meta_stride, y_meta_scanlines;
1034 	u32 uv_meta_stride, uv_meta_scanlines;
1035 	u32 y_meta_plane, uv_meta_plane;
1036 	u32 extradata = SZ_16K;
1037 	u32 size;
1038 
1039 	y_stride = ALIGN(ALIGN(width, 192) * 4 / 3, 256);
1040 	uv_stride = ALIGN(ALIGN(width, 192) * 4 / 3, 256);
1041 	y_sclines = ALIGN(height, 16);
1042 	uv_sclines = ALIGN((height + 1) >> 1, 16);
1043 
1044 	y_ubwc_plane = ALIGN(y_stride * y_sclines, SZ_4K);
1045 	uv_ubwc_plane = ALIGN(uv_stride * uv_sclines, SZ_4K);
1046 	y_meta_stride = ALIGN(DIV_ROUND_UP(width, 48), 64);
1047 	y_meta_scanlines = ALIGN(DIV_ROUND_UP(height, 4), 16);
1048 	y_meta_plane = ALIGN(y_meta_stride * y_meta_scanlines, SZ_4K);
1049 	uv_meta_stride = ALIGN(DIV_ROUND_UP((width + 1) >> 1, 24), 64);
1050 	uv_meta_scanlines = ALIGN(DIV_ROUND_UP((height + 1) >> 1, 4), 16);
1051 	uv_meta_plane = ALIGN(uv_meta_stride * uv_meta_scanlines, SZ_4K);
1052 
1053 	size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane + uv_meta_plane;
1054 	size += max(extradata + SZ_8K, y_stride * 48);
1055 
1056 	return ALIGN(size, SZ_4K);
1057 }
1058 
venus_helper_get_framesz_raw(u32 hfi_fmt,u32 width,u32 height)1059 u32 venus_helper_get_framesz_raw(u32 hfi_fmt, u32 width, u32 height)
1060 {
1061 	switch (hfi_fmt) {
1062 	case HFI_COLOR_FORMAT_NV12:
1063 	case HFI_COLOR_FORMAT_NV21:
1064 		return get_framesize_raw_nv12(width, height);
1065 	case HFI_COLOR_FORMAT_NV12_UBWC:
1066 		return get_framesize_raw_nv12_ubwc(width, height);
1067 	case HFI_COLOR_FORMAT_P010:
1068 		return get_framesize_raw_p010(width, height);
1069 	case HFI_COLOR_FORMAT_P010_UBWC:
1070 		return get_framesize_raw_p010_ubwc(width, height);
1071 	case HFI_COLOR_FORMAT_YUV420_TP10_UBWC:
1072 		return get_framesize_raw_yuv420_tp10_ubwc(width, height);
1073 	default:
1074 		return 0;
1075 	}
1076 }
1077 EXPORT_SYMBOL_GPL(venus_helper_get_framesz_raw);
1078 
venus_helper_get_framesz(u32 v4l2_fmt,u32 width,u32 height)1079 u32 venus_helper_get_framesz(u32 v4l2_fmt, u32 width, u32 height)
1080 {
1081 	u32 hfi_fmt, sz;
1082 	bool compressed;
1083 
1084 	switch (v4l2_fmt) {
1085 	case V4L2_PIX_FMT_MPEG:
1086 	case V4L2_PIX_FMT_H264:
1087 	case V4L2_PIX_FMT_H264_NO_SC:
1088 	case V4L2_PIX_FMT_H264_MVC:
1089 	case V4L2_PIX_FMT_H263:
1090 	case V4L2_PIX_FMT_MPEG1:
1091 	case V4L2_PIX_FMT_MPEG2:
1092 	case V4L2_PIX_FMT_MPEG4:
1093 	case V4L2_PIX_FMT_XVID:
1094 	case V4L2_PIX_FMT_VC1_ANNEX_G:
1095 	case V4L2_PIX_FMT_VC1_ANNEX_L:
1096 	case V4L2_PIX_FMT_VP8:
1097 	case V4L2_PIX_FMT_VP9:
1098 	case V4L2_PIX_FMT_HEVC:
1099 		compressed = true;
1100 		break;
1101 	default:
1102 		compressed = false;
1103 		break;
1104 	}
1105 
1106 	if (compressed) {
1107 		sz = ALIGN(height, 32) * ALIGN(width, 32) * 3 / 2 / 2;
1108 		if (width < 1280 || height < 720)
1109 			sz *= 8;
1110 		return ALIGN(sz, SZ_4K);
1111 	}
1112 
1113 	hfi_fmt = to_hfi_raw_fmt(v4l2_fmt);
1114 	if (!hfi_fmt)
1115 		return 0;
1116 
1117 	return venus_helper_get_framesz_raw(hfi_fmt, width, height);
1118 }
1119 EXPORT_SYMBOL_GPL(venus_helper_get_framesz);
1120 
venus_helper_set_input_resolution(struct venus_inst * inst,unsigned int width,unsigned int height)1121 int venus_helper_set_input_resolution(struct venus_inst *inst,
1122 				      unsigned int width, unsigned int height)
1123 {
1124 	u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
1125 	struct hfi_framesize fs;
1126 
1127 	fs.buffer_type = HFI_BUFFER_INPUT;
1128 	fs.width = width;
1129 	fs.height = height;
1130 
1131 	return hfi_session_set_property(inst, ptype, &fs);
1132 }
1133 EXPORT_SYMBOL_GPL(venus_helper_set_input_resolution);
1134 
venus_helper_set_output_resolution(struct venus_inst * inst,unsigned int width,unsigned int height,u32 buftype)1135 int venus_helper_set_output_resolution(struct venus_inst *inst,
1136 				       unsigned int width, unsigned int height,
1137 				       u32 buftype)
1138 {
1139 	u32 ptype = HFI_PROPERTY_PARAM_FRAME_SIZE;
1140 	struct hfi_framesize fs;
1141 
1142 	fs.buffer_type = buftype;
1143 	fs.width = width;
1144 	fs.height = height;
1145 
1146 	return hfi_session_set_property(inst, ptype, &fs);
1147 }
1148 EXPORT_SYMBOL_GPL(venus_helper_set_output_resolution);
1149 
venus_helper_get_work_mode(struct venus_inst * inst)1150 static u32 venus_helper_get_work_mode(struct venus_inst *inst)
1151 {
1152 	u32 mode;
1153 	u32 num_mbs;
1154 
1155 	mode = VIDC_WORK_MODE_2;
1156 	if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
1157 		num_mbs = (ALIGN(inst->height, 16) * ALIGN(inst->width, 16)) / 256;
1158 		if (inst->hfi_codec == HFI_VIDEO_CODEC_MPEG2 ||
1159 		    inst->pic_struct != HFI_INTERLACE_FRAME_PROGRESSIVE ||
1160 		    num_mbs <= NUM_MBS_720P)
1161 			mode = VIDC_WORK_MODE_1;
1162 	} else {
1163 		num_mbs = (ALIGN(inst->out_height, 16) * ALIGN(inst->out_width, 16)) / 256;
1164 		if (inst->hfi_codec == HFI_VIDEO_CODEC_VP8 &&
1165 		    num_mbs <= NUM_MBS_4K)
1166 			mode = VIDC_WORK_MODE_1;
1167 	}
1168 
1169 	return mode;
1170 }
1171 
venus_helper_set_work_mode(struct venus_inst * inst)1172 int venus_helper_set_work_mode(struct venus_inst *inst)
1173 {
1174 	const u32 ptype = HFI_PROPERTY_PARAM_WORK_MODE;
1175 	struct hfi_video_work_mode wm;
1176 	u32 mode;
1177 
1178 	if (!IS_V4(inst->core) && !IS_V6(inst->core))
1179 		return 0;
1180 
1181 	mode = venus_helper_get_work_mode(inst);
1182 	wm.video_work_mode = mode;
1183 	return hfi_session_set_property(inst, ptype, &wm);
1184 }
1185 EXPORT_SYMBOL_GPL(venus_helper_set_work_mode);
1186 
venus_helper_set_format_constraints(struct venus_inst * inst)1187 int venus_helper_set_format_constraints(struct venus_inst *inst)
1188 {
1189 	const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_PLANE_ACTUAL_CONSTRAINTS_INFO;
1190 	struct hfi_uncompressed_plane_actual_constraints_info pconstraint;
1191 
1192 	if (!IS_V6(inst->core))
1193 		return 0;
1194 
1195 	if (inst->opb_fmt == HFI_COLOR_FORMAT_NV12_UBWC ||
1196 	    inst->opb_fmt == HFI_COLOR_FORMAT_YUV420_TP10_UBWC)
1197 		return 0;
1198 
1199 	pconstraint.buffer_type = HFI_BUFFER_OUTPUT2;
1200 	pconstraint.num_planes = 2;
1201 	pconstraint.plane_format[0].stride_multiples = 128;
1202 	pconstraint.plane_format[0].max_stride = 8192;
1203 	pconstraint.plane_format[0].min_plane_buffer_height_multiple = 32;
1204 	pconstraint.plane_format[0].buffer_alignment = 256;
1205 
1206 	pconstraint.plane_format[1].stride_multiples = 128;
1207 	pconstraint.plane_format[1].max_stride = 8192;
1208 	pconstraint.plane_format[1].min_plane_buffer_height_multiple = 16;
1209 	pconstraint.plane_format[1].buffer_alignment = 256;
1210 
1211 	return hfi_session_set_property(inst, ptype, &pconstraint);
1212 }
1213 EXPORT_SYMBOL_GPL(venus_helper_set_format_constraints);
1214 
venus_helper_set_num_bufs(struct venus_inst * inst,unsigned int input_bufs,unsigned int output_bufs,unsigned int output2_bufs)1215 int venus_helper_set_num_bufs(struct venus_inst *inst, unsigned int input_bufs,
1216 			      unsigned int output_bufs,
1217 			      unsigned int output2_bufs)
1218 {
1219 	u32 ptype = HFI_PROPERTY_PARAM_BUFFER_COUNT_ACTUAL;
1220 	struct hfi_buffer_count_actual buf_count;
1221 	int ret;
1222 
1223 	buf_count.type = HFI_BUFFER_INPUT;
1224 	buf_count.count_actual = input_bufs;
1225 
1226 	ret = hfi_session_set_property(inst, ptype, &buf_count);
1227 	if (ret)
1228 		return ret;
1229 
1230 	buf_count.type = HFI_BUFFER_OUTPUT;
1231 	buf_count.count_actual = output_bufs;
1232 
1233 	ret = hfi_session_set_property(inst, ptype, &buf_count);
1234 	if (ret)
1235 		return ret;
1236 
1237 	if (output2_bufs) {
1238 		buf_count.type = HFI_BUFFER_OUTPUT2;
1239 		buf_count.count_actual = output2_bufs;
1240 
1241 		ret = hfi_session_set_property(inst, ptype, &buf_count);
1242 	}
1243 
1244 	return ret;
1245 }
1246 EXPORT_SYMBOL_GPL(venus_helper_set_num_bufs);
1247 
venus_helper_set_raw_format(struct venus_inst * inst,u32 hfi_format,u32 buftype)1248 int venus_helper_set_raw_format(struct venus_inst *inst, u32 hfi_format,
1249 				u32 buftype)
1250 {
1251 	const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_FORMAT_SELECT;
1252 	struct hfi_uncompressed_format_select fmt;
1253 
1254 	fmt.buffer_type = buftype;
1255 	fmt.format = hfi_format;
1256 
1257 	return hfi_session_set_property(inst, ptype, &fmt);
1258 }
1259 EXPORT_SYMBOL_GPL(venus_helper_set_raw_format);
1260 
venus_helper_set_color_format(struct venus_inst * inst,u32 pixfmt)1261 int venus_helper_set_color_format(struct venus_inst *inst, u32 pixfmt)
1262 {
1263 	u32 hfi_format, buftype;
1264 
1265 	if (inst->session_type == VIDC_SESSION_TYPE_DEC)
1266 		buftype = HFI_BUFFER_OUTPUT;
1267 	else if (inst->session_type == VIDC_SESSION_TYPE_ENC)
1268 		buftype = HFI_BUFFER_INPUT;
1269 	else
1270 		return -EINVAL;
1271 
1272 	hfi_format = to_hfi_raw_fmt(pixfmt);
1273 	if (!hfi_format)
1274 		return -EINVAL;
1275 
1276 	return venus_helper_set_raw_format(inst, hfi_format, buftype);
1277 }
1278 EXPORT_SYMBOL_GPL(venus_helper_set_color_format);
1279 
venus_helper_set_multistream(struct venus_inst * inst,bool out_en,bool out2_en)1280 int venus_helper_set_multistream(struct venus_inst *inst, bool out_en,
1281 				 bool out2_en)
1282 {
1283 	struct hfi_multi_stream multi = {0};
1284 	u32 ptype = HFI_PROPERTY_PARAM_VDEC_MULTI_STREAM;
1285 	int ret;
1286 
1287 	multi.buffer_type = HFI_BUFFER_OUTPUT;
1288 	multi.enable = out_en;
1289 
1290 	ret = hfi_session_set_property(inst, ptype, &multi);
1291 	if (ret)
1292 		return ret;
1293 
1294 	multi.buffer_type = HFI_BUFFER_OUTPUT2;
1295 	multi.enable = out2_en;
1296 
1297 	return hfi_session_set_property(inst, ptype, &multi);
1298 }
1299 EXPORT_SYMBOL_GPL(venus_helper_set_multistream);
1300 
venus_helper_set_dyn_bufmode(struct venus_inst * inst)1301 int venus_helper_set_dyn_bufmode(struct venus_inst *inst)
1302 {
1303 	const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_ALLOC_MODE;
1304 	struct hfi_buffer_alloc_mode mode;
1305 	int ret;
1306 
1307 	if (!is_dynamic_bufmode(inst))
1308 		return 0;
1309 
1310 	mode.type = HFI_BUFFER_OUTPUT;
1311 	mode.mode = HFI_BUFFER_MODE_DYNAMIC;
1312 
1313 	ret = hfi_session_set_property(inst, ptype, &mode);
1314 	if (ret)
1315 		return ret;
1316 
1317 	mode.type = HFI_BUFFER_OUTPUT2;
1318 
1319 	return hfi_session_set_property(inst, ptype, &mode);
1320 }
1321 EXPORT_SYMBOL_GPL(venus_helper_set_dyn_bufmode);
1322 
venus_helper_set_bufsize(struct venus_inst * inst,u32 bufsize,u32 buftype)1323 int venus_helper_set_bufsize(struct venus_inst *inst, u32 bufsize, u32 buftype)
1324 {
1325 	const u32 ptype = HFI_PROPERTY_PARAM_BUFFER_SIZE_ACTUAL;
1326 	struct hfi_buffer_size_actual bufsz;
1327 
1328 	bufsz.type = buftype;
1329 	bufsz.size = bufsize;
1330 
1331 	return hfi_session_set_property(inst, ptype, &bufsz);
1332 }
1333 EXPORT_SYMBOL_GPL(venus_helper_set_bufsize);
1334 
venus_helper_get_opb_size(struct venus_inst * inst)1335 unsigned int venus_helper_get_opb_size(struct venus_inst *inst)
1336 {
1337 	/* the encoder has only one output */
1338 	if (inst->session_type == VIDC_SESSION_TYPE_ENC)
1339 		return inst->output_buf_size;
1340 
1341 	if (inst->opb_buftype == HFI_BUFFER_OUTPUT)
1342 		return inst->output_buf_size;
1343 	else if (inst->opb_buftype == HFI_BUFFER_OUTPUT2)
1344 		return inst->output2_buf_size;
1345 
1346 	return 0;
1347 }
1348 EXPORT_SYMBOL_GPL(venus_helper_get_opb_size);
1349 
delayed_process_buf_func(struct work_struct * work)1350 static void delayed_process_buf_func(struct work_struct *work)
1351 {
1352 	struct venus_buffer *buf, *n;
1353 	struct venus_inst *inst;
1354 	int ret;
1355 
1356 	inst = container_of(work, struct venus_inst, delayed_process_work);
1357 
1358 	mutex_lock(&inst->lock);
1359 
1360 	if (!(inst->streamon_out & inst->streamon_cap))
1361 		goto unlock;
1362 
1363 	list_for_each_entry_safe(buf, n, &inst->delayed_process, ref_list) {
1364 		if (buf->flags & HFI_BUFFERFLAG_READONLY)
1365 			continue;
1366 
1367 		ret = session_process_buf(inst, &buf->vb);
1368 		if (ret)
1369 			return_buf_error(inst, &buf->vb);
1370 
1371 		list_del_init(&buf->ref_list);
1372 	}
1373 unlock:
1374 	mutex_unlock(&inst->lock);
1375 }
1376 
venus_helper_release_buf_ref(struct venus_inst * inst,unsigned int idx)1377 void venus_helper_release_buf_ref(struct venus_inst *inst, unsigned int idx)
1378 {
1379 	struct venus_buffer *buf;
1380 
1381 	list_for_each_entry(buf, &inst->registeredbufs, reg_list) {
1382 		if (buf->vb.vb2_buf.index == idx) {
1383 			buf->flags &= ~HFI_BUFFERFLAG_READONLY;
1384 			schedule_work(&inst->delayed_process_work);
1385 			break;
1386 		}
1387 	}
1388 }
1389 EXPORT_SYMBOL_GPL(venus_helper_release_buf_ref);
1390 
venus_helper_acquire_buf_ref(struct vb2_v4l2_buffer * vbuf)1391 void venus_helper_acquire_buf_ref(struct vb2_v4l2_buffer *vbuf)
1392 {
1393 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1394 
1395 	buf->flags |= HFI_BUFFERFLAG_READONLY;
1396 }
1397 EXPORT_SYMBOL_GPL(venus_helper_acquire_buf_ref);
1398 
is_buf_refed(struct venus_inst * inst,struct vb2_v4l2_buffer * vbuf)1399 static int is_buf_refed(struct venus_inst *inst, struct vb2_v4l2_buffer *vbuf)
1400 {
1401 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1402 
1403 	if (buf->flags & HFI_BUFFERFLAG_READONLY) {
1404 		list_add_tail(&buf->ref_list, &inst->delayed_process);
1405 		schedule_work(&inst->delayed_process_work);
1406 		return 1;
1407 	}
1408 
1409 	return 0;
1410 }
1411 
1412 struct vb2_v4l2_buffer *
venus_helper_find_buf(struct venus_inst * inst,unsigned int type,u32 idx)1413 venus_helper_find_buf(struct venus_inst *inst, unsigned int type, u32 idx)
1414 {
1415 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1416 
1417 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1418 		return v4l2_m2m_src_buf_remove_by_idx(m2m_ctx, idx);
1419 	else
1420 		return v4l2_m2m_dst_buf_remove_by_idx(m2m_ctx, idx);
1421 }
1422 EXPORT_SYMBOL_GPL(venus_helper_find_buf);
1423 
venus_helper_change_dpb_owner(struct venus_inst * inst,struct vb2_v4l2_buffer * vbuf,unsigned int type,unsigned int buf_type,u32 tag)1424 void venus_helper_change_dpb_owner(struct venus_inst *inst,
1425 				   struct vb2_v4l2_buffer *vbuf, unsigned int type,
1426 				   unsigned int buf_type, u32 tag)
1427 {
1428 	struct intbuf *dpb_buf;
1429 
1430 	if (!V4L2_TYPE_IS_CAPTURE(type) ||
1431 	    buf_type != inst->dpb_buftype)
1432 		return;
1433 
1434 	list_for_each_entry(dpb_buf, &inst->dpbbufs, list)
1435 		if (dpb_buf->dpb_out_tag == tag) {
1436 			dpb_buf->owned_by = DRIVER;
1437 			break;
1438 		}
1439 }
1440 EXPORT_SYMBOL_GPL(venus_helper_change_dpb_owner);
1441 
venus_helper_vb2_buf_init(struct vb2_buffer * vb)1442 int venus_helper_vb2_buf_init(struct vb2_buffer *vb)
1443 {
1444 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1445 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1446 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1447 
1448 	buf->size = vb2_plane_size(vb, 0);
1449 	buf->dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1450 
1451 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1452 		list_add_tail(&buf->reg_list, &inst->registeredbufs);
1453 
1454 	return 0;
1455 }
1456 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_init);
1457 
venus_helper_vb2_buf_prepare(struct vb2_buffer * vb)1458 int venus_helper_vb2_buf_prepare(struct vb2_buffer *vb)
1459 {
1460 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1461 	unsigned int out_buf_size = venus_helper_get_opb_size(inst);
1462 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1463 
1464 	if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1465 		if (vbuf->field == V4L2_FIELD_ANY)
1466 			vbuf->field = V4L2_FIELD_NONE;
1467 		if (vbuf->field != V4L2_FIELD_NONE) {
1468 			dev_err(inst->core->dev, "%s field isn't supported\n",
1469 				__func__);
1470 			return -EINVAL;
1471 		}
1472 	}
1473 
1474 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
1475 	    vb2_plane_size(vb, 0) < out_buf_size)
1476 		return -EINVAL;
1477 	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1478 	    vb2_plane_size(vb, 0) < inst->input_buf_size)
1479 		return -EINVAL;
1480 
1481 	return 0;
1482 }
1483 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_prepare);
1484 
cache_payload(struct venus_inst * inst,struct vb2_buffer * vb)1485 static void cache_payload(struct venus_inst *inst, struct vb2_buffer *vb)
1486 {
1487 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1488 	unsigned int idx = vbuf->vb2_buf.index;
1489 
1490 	if (vbuf->vb2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1491 		inst->payloads[idx] = vb2_get_plane_payload(vb, 0);
1492 }
1493 
venus_helper_vb2_buf_queue(struct vb2_buffer * vb)1494 void venus_helper_vb2_buf_queue(struct vb2_buffer *vb)
1495 {
1496 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1497 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1498 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1499 	int ret;
1500 
1501 	v4l2_m2m_buf_queue(m2m_ctx, vbuf);
1502 
1503 	/* Skip processing queued capture buffers after LAST flag */
1504 	if (inst->session_type == VIDC_SESSION_TYPE_DEC &&
1505 	    V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1506 	    inst->codec_state == VENUS_DEC_STATE_DRC)
1507 		return;
1508 
1509 	cache_payload(inst, vb);
1510 
1511 	if (inst->session_type == VIDC_SESSION_TYPE_ENC &&
1512 	    !(inst->streamon_out && inst->streamon_cap))
1513 		return;
1514 
1515 	if (vb2_start_streaming_called(vb->vb2_queue)) {
1516 		ret = is_buf_refed(inst, vbuf);
1517 		if (ret)
1518 			return;
1519 
1520 		ret = session_process_buf(inst, vbuf);
1521 		if (ret)
1522 			return_buf_error(inst, vbuf);
1523 	}
1524 }
1525 EXPORT_SYMBOL_GPL(venus_helper_vb2_buf_queue);
1526 
venus_helper_buffers_done(struct venus_inst * inst,unsigned int type,enum vb2_buffer_state state)1527 void venus_helper_buffers_done(struct venus_inst *inst, unsigned int type,
1528 			       enum vb2_buffer_state state)
1529 {
1530 	struct vb2_v4l2_buffer *buf;
1531 
1532 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1533 		while ((buf = v4l2_m2m_src_buf_remove(inst->m2m_ctx)))
1534 			v4l2_m2m_buf_done(buf, state);
1535 	} else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1536 		while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1537 			v4l2_m2m_buf_done(buf, state);
1538 	}
1539 }
1540 EXPORT_SYMBOL_GPL(venus_helper_buffers_done);
1541 
venus_helper_vb2_stop_streaming(struct vb2_queue * q)1542 void venus_helper_vb2_stop_streaming(struct vb2_queue *q)
1543 {
1544 	struct venus_inst *inst = vb2_get_drv_priv(q);
1545 	struct venus_core *core = inst->core;
1546 	int ret;
1547 
1548 	mutex_lock(&inst->lock);
1549 
1550 	if (inst->streamon_out & inst->streamon_cap) {
1551 		ret = hfi_session_stop(inst);
1552 		ret |= hfi_session_unload_res(inst);
1553 		ret |= venus_helper_unregister_bufs(inst);
1554 		ret |= venus_helper_intbufs_free(inst);
1555 		ret |= hfi_session_deinit(inst);
1556 
1557 		if (inst->session_error || test_bit(0, &core->sys_error))
1558 			ret = -EIO;
1559 
1560 		if (ret)
1561 			hfi_session_abort(inst);
1562 
1563 		venus_helper_free_dpb_bufs(inst);
1564 
1565 		venus_pm_load_scale(inst);
1566 		INIT_LIST_HEAD(&inst->registeredbufs);
1567 	}
1568 
1569 	venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
1570 				  VB2_BUF_STATE_ERROR);
1571 	venus_helper_buffers_done(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1572 				  VB2_BUF_STATE_ERROR);
1573 
1574 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1575 		inst->streamon_out = 0;
1576 	else
1577 		inst->streamon_cap = 0;
1578 
1579 	venus_pm_release_core(inst);
1580 
1581 	inst->session_error = 0;
1582 
1583 	mutex_unlock(&inst->lock);
1584 }
1585 EXPORT_SYMBOL_GPL(venus_helper_vb2_stop_streaming);
1586 
venus_helper_vb2_queue_error(struct venus_inst * inst)1587 void venus_helper_vb2_queue_error(struct venus_inst *inst)
1588 {
1589 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1590 	struct vb2_queue *q;
1591 
1592 	q = v4l2_m2m_get_src_vq(m2m_ctx);
1593 	vb2_queue_error(q);
1594 	q = v4l2_m2m_get_dst_vq(m2m_ctx);
1595 	vb2_queue_error(q);
1596 }
1597 EXPORT_SYMBOL_GPL(venus_helper_vb2_queue_error);
1598 
venus_helper_process_initial_cap_bufs(struct venus_inst * inst)1599 int venus_helper_process_initial_cap_bufs(struct venus_inst *inst)
1600 {
1601 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1602 	struct v4l2_m2m_buffer *buf, *n;
1603 	int ret;
1604 
1605 	v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
1606 		ret = session_process_buf(inst, &buf->vb);
1607 		if (ret) {
1608 			return_buf_error(inst, &buf->vb);
1609 			return ret;
1610 		}
1611 	}
1612 
1613 	return 0;
1614 }
1615 EXPORT_SYMBOL_GPL(venus_helper_process_initial_cap_bufs);
1616 
venus_helper_process_initial_out_bufs(struct venus_inst * inst)1617 int venus_helper_process_initial_out_bufs(struct venus_inst *inst)
1618 {
1619 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1620 	struct v4l2_m2m_buffer *buf, *n;
1621 	int ret;
1622 
1623 	v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
1624 		ret = session_process_buf(inst, &buf->vb);
1625 		if (ret) {
1626 			return_buf_error(inst, &buf->vb);
1627 			return ret;
1628 		}
1629 	}
1630 
1631 	return 0;
1632 }
1633 EXPORT_SYMBOL_GPL(venus_helper_process_initial_out_bufs);
1634 
venus_helper_vb2_start_streaming(struct venus_inst * inst)1635 int venus_helper_vb2_start_streaming(struct venus_inst *inst)
1636 {
1637 	int ret;
1638 
1639 	ret = venus_helper_intbufs_alloc(inst);
1640 	if (ret)
1641 		return ret;
1642 
1643 	ret = session_register_bufs(inst);
1644 	if (ret)
1645 		goto err_bufs_free;
1646 
1647 	venus_pm_load_scale(inst);
1648 
1649 	ret = hfi_session_load_res(inst);
1650 	if (ret)
1651 		goto err_unreg_bufs;
1652 
1653 	ret = hfi_session_start(inst);
1654 	if (ret)
1655 		goto err_unload_res;
1656 
1657 	return 0;
1658 
1659 err_unload_res:
1660 	hfi_session_unload_res(inst);
1661 err_unreg_bufs:
1662 	venus_helper_unregister_bufs(inst);
1663 err_bufs_free:
1664 	venus_helper_intbufs_free(inst);
1665 	return ret;
1666 }
1667 EXPORT_SYMBOL_GPL(venus_helper_vb2_start_streaming);
1668 
venus_helper_m2m_device_run(void * priv)1669 void venus_helper_m2m_device_run(void *priv)
1670 {
1671 	struct venus_inst *inst = priv;
1672 	struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
1673 	struct v4l2_m2m_buffer *buf, *n;
1674 	int ret;
1675 
1676 	mutex_lock(&inst->lock);
1677 
1678 	v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buf, n) {
1679 		ret = session_process_buf(inst, &buf->vb);
1680 		if (ret)
1681 			return_buf_error(inst, &buf->vb);
1682 	}
1683 
1684 	v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buf, n) {
1685 		ret = session_process_buf(inst, &buf->vb);
1686 		if (ret)
1687 			return_buf_error(inst, &buf->vb);
1688 	}
1689 
1690 	mutex_unlock(&inst->lock);
1691 }
1692 EXPORT_SYMBOL_GPL(venus_helper_m2m_device_run);
1693 
venus_helper_m2m_job_abort(void * priv)1694 void venus_helper_m2m_job_abort(void *priv)
1695 {
1696 	struct venus_inst *inst = priv;
1697 
1698 	v4l2_m2m_job_finish(inst->m2m_dev, inst->m2m_ctx);
1699 }
1700 EXPORT_SYMBOL_GPL(venus_helper_m2m_job_abort);
1701 
venus_helper_session_init(struct venus_inst * inst)1702 int venus_helper_session_init(struct venus_inst *inst)
1703 {
1704 	enum hfi_version version = inst->core->res->hfi_version;
1705 	u32 session_type = inst->session_type;
1706 	u32 codec;
1707 	int ret;
1708 
1709 	codec = inst->session_type == VIDC_SESSION_TYPE_DEC ?
1710 			inst->fmt_out->pixfmt : inst->fmt_cap->pixfmt;
1711 
1712 	ret = hfi_session_init(inst, codec);
1713 	if (ret)
1714 		return ret;
1715 
1716 	inst->clk_data.vpp_freq = hfi_platform_get_codec_vpp_freq(version, codec,
1717 								  session_type);
1718 	inst->clk_data.vsp_freq = hfi_platform_get_codec_vsp_freq(version, codec,
1719 								  session_type);
1720 	inst->clk_data.low_power_freq = hfi_platform_get_codec_lp_freq(version, codec,
1721 								       session_type);
1722 
1723 	return 0;
1724 }
1725 EXPORT_SYMBOL_GPL(venus_helper_session_init);
1726 
venus_helper_init_instance(struct venus_inst * inst)1727 void venus_helper_init_instance(struct venus_inst *inst)
1728 {
1729 	if (inst->session_type == VIDC_SESSION_TYPE_DEC) {
1730 		INIT_LIST_HEAD(&inst->delayed_process);
1731 		INIT_WORK(&inst->delayed_process_work,
1732 			  delayed_process_buf_func);
1733 	}
1734 }
1735 EXPORT_SYMBOL_GPL(venus_helper_init_instance);
1736 
find_fmt_from_caps(struct hfi_plat_caps * caps,u32 buftype,u32 fmt)1737 static bool find_fmt_from_caps(struct hfi_plat_caps *caps, u32 buftype, u32 fmt)
1738 {
1739 	unsigned int i;
1740 
1741 	for (i = 0; i < caps->num_fmts; i++) {
1742 		if (caps->fmts[i].buftype == buftype &&
1743 		    caps->fmts[i].fmt == fmt)
1744 			return true;
1745 	}
1746 
1747 	return false;
1748 }
1749 
venus_helper_get_out_fmts(struct venus_inst * inst,u32 v4l2_fmt,u32 * out_fmt,u32 * out2_fmt,bool ubwc)1750 int venus_helper_get_out_fmts(struct venus_inst *inst, u32 v4l2_fmt,
1751 			      u32 *out_fmt, u32 *out2_fmt, bool ubwc)
1752 {
1753 	struct venus_core *core = inst->core;
1754 	struct hfi_plat_caps *caps;
1755 	u32 ubwc_fmt, fmt = to_hfi_raw_fmt(v4l2_fmt);
1756 	bool found, found_ubwc;
1757 
1758 	*out_fmt = *out2_fmt = 0;
1759 
1760 	if (!fmt)
1761 		return -EINVAL;
1762 
1763 	caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
1764 	if (!caps)
1765 		return -EINVAL;
1766 
1767 	if (ubwc) {
1768 		ubwc_fmt = fmt | HFI_COLOR_FORMAT_UBWC_BASE;
1769 		found_ubwc = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT,
1770 						ubwc_fmt);
1771 		found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1772 
1773 		if (found_ubwc && found) {
1774 			*out_fmt = ubwc_fmt;
1775 			*out2_fmt = fmt;
1776 			return 0;
1777 		}
1778 	}
1779 
1780 	found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT, fmt);
1781 	if (found) {
1782 		*out_fmt = fmt;
1783 		*out2_fmt = 0;
1784 		return 0;
1785 	}
1786 
1787 	found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1788 	if (found) {
1789 		*out_fmt = 0;
1790 		*out2_fmt = fmt;
1791 		return 0;
1792 	}
1793 
1794 	return -EINVAL;
1795 }
1796 EXPORT_SYMBOL_GPL(venus_helper_get_out_fmts);
1797 
venus_helper_check_format(struct venus_inst * inst,u32 v4l2_pixfmt)1798 bool venus_helper_check_format(struct venus_inst *inst, u32 v4l2_pixfmt)
1799 {
1800 	struct venus_core *core = inst->core;
1801 	u32 fmt = to_hfi_raw_fmt(v4l2_pixfmt);
1802 	struct hfi_plat_caps *caps;
1803 	bool found;
1804 
1805 	if (!fmt)
1806 		return false;
1807 
1808 	caps = venus_caps_by_codec(core, inst->hfi_codec, inst->session_type);
1809 	if (!caps)
1810 		return false;
1811 
1812 	found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT, fmt);
1813 	if (found)
1814 		goto done;
1815 
1816 	found = find_fmt_from_caps(caps, HFI_BUFFER_OUTPUT2, fmt);
1817 done:
1818 	return found;
1819 }
1820 EXPORT_SYMBOL_GPL(venus_helper_check_format);
1821 
venus_helper_set_stride(struct venus_inst * inst,unsigned int width,unsigned int height)1822 int venus_helper_set_stride(struct venus_inst *inst,
1823 			    unsigned int width, unsigned int height)
1824 {
1825 	const u32 ptype = HFI_PROPERTY_PARAM_UNCOMPRESSED_PLANE_ACTUAL_INFO;
1826 
1827 	struct hfi_uncompressed_plane_actual_info plane_actual_info;
1828 
1829 	plane_actual_info.buffer_type = HFI_BUFFER_INPUT;
1830 	plane_actual_info.num_planes = 2;
1831 	plane_actual_info.plane_format[0].actual_stride = width;
1832 	plane_actual_info.plane_format[0].actual_plane_buffer_height = height;
1833 	plane_actual_info.plane_format[1].actual_stride = width;
1834 	plane_actual_info.plane_format[1].actual_plane_buffer_height = height / 2;
1835 
1836 	return hfi_session_set_property(inst, ptype, &plane_actual_info);
1837 }
1838 EXPORT_SYMBOL_GPL(venus_helper_set_stride);
1839