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