1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020-2021 NXP
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/videodev2.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-mem2mem.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/videobuf2-v4l2.h>
19 #include <media/videobuf2-dma-contig.h>
20 #include <media/videobuf2-vmalloc.h>
25 #include "vpu_helpers.h"
27 void vpu_inst_lock(struct vpu_inst *inst)
29 mutex_lock(&inst->lock);
32 void vpu_inst_unlock(struct vpu_inst *inst)
34 mutex_unlock(&inst->lock);
37 dma_addr_t vpu_get_vb_phy_addr(struct vb2_buffer *vb, u32 plane_no)
39 if (plane_no >= vb->num_planes)
41 return vb2_dma_contig_plane_dma_addr(vb, plane_no) +
42 vb->planes[plane_no].data_offset;
45 unsigned int vpu_get_vb_length(struct vb2_buffer *vb, u32 plane_no)
47 if (plane_no >= vb->num_planes)
49 return vb2_plane_size(vb, plane_no) - vb->planes[plane_no].data_offset;
52 void vpu_set_buffer_state(struct vb2_v4l2_buffer *vbuf, unsigned int state)
54 struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf);
56 vpu_buf->state = state;
59 unsigned int vpu_get_buffer_state(struct vb2_v4l2_buffer *vbuf)
61 struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf);
63 return vpu_buf->state;
66 void vpu_v4l2_set_error(struct vpu_inst *inst)
69 dev_err(inst->dev, "some error occurs in codec\n");
70 if (inst->fh.m2m_ctx) {
71 vb2_queue_error(v4l2_m2m_get_src_vq(inst->fh.m2m_ctx));
72 vb2_queue_error(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx));
74 vpu_inst_unlock(inst);
77 int vpu_notify_eos(struct vpu_inst *inst)
79 static const struct v4l2_event ev = {
81 .type = V4L2_EVENT_EOS
84 vpu_trace(inst->dev, "[%d]\n", inst->id);
85 v4l2_event_queue_fh(&inst->fh, &ev);
90 int vpu_notify_source_change(struct vpu_inst *inst)
92 static const struct v4l2_event ev = {
94 .type = V4L2_EVENT_SOURCE_CHANGE,
95 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION
98 vpu_trace(inst->dev, "[%d]\n", inst->id);
99 v4l2_event_queue_fh(&inst->fh, &ev);
103 int vpu_set_last_buffer_dequeued(struct vpu_inst *inst)
107 if (!inst || !inst->fh.m2m_ctx)
110 q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
111 if (!list_empty(&q->done_list))
114 if (q->last_buffer_dequeued)
116 vpu_trace(inst->dev, "last buffer dequeued\n");
117 q->last_buffer_dequeued = true;
118 wake_up(&q->done_wq);
119 vpu_notify_eos(inst);
123 bool vpu_is_source_empty(struct vpu_inst *inst)
125 struct v4l2_m2m_buffer *buf = NULL;
127 if (!inst->fh.m2m_ctx)
129 v4l2_m2m_for_each_src_buf(inst->fh.m2m_ctx, buf) {
130 if (vpu_get_buffer_state(&buf->vb) == VPU_BUF_STATE_IDLE)
136 static int vpu_init_format(struct vpu_inst *inst, struct vpu_format *fmt)
138 const struct vpu_format *info;
140 info = vpu_helper_find_format(inst, fmt->type, fmt->pixfmt);
142 info = vpu_helper_enum_format(inst, fmt->type, 0);
146 memcpy(fmt, info, sizeof(*fmt));
151 static int vpu_calc_fmt_bytesperline(struct v4l2_format *f, struct vpu_format *fmt)
153 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
156 if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
157 for (i = 0; i < fmt->comp_planes; i++)
158 fmt->bytesperline[i] = 0;
161 if (pixmp->num_planes == fmt->comp_planes) {
162 for (i = 0; i < fmt->comp_planes; i++)
163 fmt->bytesperline[i] = pixmp->plane_fmt[i].bytesperline;
166 if (pixmp->num_planes > 1)
169 /*amphion vpu only support nv12 and nv12 tiled,
170 * so the bytesperline of luma and chroma should be same
172 for (i = 0; i < fmt->comp_planes; i++)
173 fmt->bytesperline[i] = pixmp->plane_fmt[0].bytesperline;
178 static int vpu_calc_fmt_sizeimage(struct vpu_inst *inst, struct vpu_format *fmt)
183 if (!(fmt->flags & V4L2_FMT_FLAG_COMPRESSED)) {
184 const struct vpu_core_resources *res = vpu_get_resource(inst);
187 stride = res->stride;
190 for (i = 0; i < fmt->comp_planes; i++) {
191 fmt->sizeimage[i] = vpu_helper_get_plane_size(fmt->pixfmt,
196 fmt->field != V4L2_FIELD_NONE ? 1 : 0,
197 &fmt->bytesperline[i]);
198 fmt->sizeimage[i] = max_t(u32, fmt->sizeimage[i], PAGE_SIZE);
199 if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
200 fmt->sizeimage[i] = clamp_val(fmt->sizeimage[i], SZ_128K, SZ_8M);
201 fmt->bytesperline[i] = 0;
208 u32 vpu_get_fmt_plane_size(struct vpu_format *fmt, u32 plane_no)
213 if (plane_no >= fmt->mem_planes)
216 if (fmt->comp_planes == fmt->mem_planes)
217 return fmt->sizeimage[plane_no];
218 if (plane_no < fmt->mem_planes - 1)
219 return fmt->sizeimage[plane_no];
221 size = fmt->sizeimage[plane_no];
222 for (i = fmt->mem_planes; i < fmt->comp_planes; i++)
223 size += fmt->sizeimage[i];
228 int vpu_try_fmt_common(struct vpu_inst *inst, struct v4l2_format *f, struct vpu_format *fmt)
230 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
234 fmt->pixfmt = pixmp->pixelformat;
236 ret = vpu_init_format(inst, fmt);
240 fmt->width = pixmp->width;
241 fmt->height = pixmp->height;
243 fmt->width = vpu_helper_valid_frame_width(inst, fmt->width);
245 fmt->height = vpu_helper_valid_frame_height(inst, fmt->height);
246 fmt->field = pixmp->field == V4L2_FIELD_ANY ? V4L2_FIELD_NONE : pixmp->field;
247 vpu_calc_fmt_bytesperline(f, fmt);
248 vpu_calc_fmt_sizeimage(inst, fmt);
249 if ((fmt->flags & V4L2_FMT_FLAG_COMPRESSED) && pixmp->plane_fmt[0].sizeimage)
250 fmt->sizeimage[0] = clamp_val(pixmp->plane_fmt[0].sizeimage, SZ_128K, SZ_8M);
252 pixmp->pixelformat = fmt->pixfmt;
253 pixmp->width = fmt->width;
254 pixmp->height = fmt->height;
255 pixmp->flags = fmt->flags;
256 pixmp->num_planes = fmt->mem_planes;
257 pixmp->field = fmt->field;
258 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
259 for (i = 0; i < pixmp->num_planes; i++) {
260 pixmp->plane_fmt[i].bytesperline = fmt->bytesperline[i];
261 pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(fmt, i);
262 memset(pixmp->plane_fmt[i].reserved, 0, sizeof(pixmp->plane_fmt[i].reserved));
268 static bool vpu_check_ready(struct vpu_inst *inst, u32 type)
272 if (inst->state == VPU_CODEC_STATE_DEINIT || inst->id < 0)
274 if (!inst->ops->check_ready)
276 return call_vop(inst, check_ready, type);
279 int vpu_process_output_buffer(struct vpu_inst *inst)
281 struct v4l2_m2m_buffer *buf = NULL;
282 struct vb2_v4l2_buffer *vbuf = NULL;
284 if (!inst || !inst->fh.m2m_ctx)
287 if (!vpu_check_ready(inst, inst->out_format.type))
290 v4l2_m2m_for_each_src_buf(inst->fh.m2m_ctx, buf) {
292 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_IDLE)
300 dev_dbg(inst->dev, "[%d]frame id = %d / %d\n",
301 inst->id, vbuf->sequence, inst->sequence);
302 return call_vop(inst, process_output, &vbuf->vb2_buf);
305 int vpu_process_capture_buffer(struct vpu_inst *inst)
307 struct v4l2_m2m_buffer *buf = NULL;
308 struct vb2_v4l2_buffer *vbuf = NULL;
310 if (!inst || !inst->fh.m2m_ctx)
313 if (!vpu_check_ready(inst, inst->cap_format.type))
316 v4l2_m2m_for_each_dst_buf(inst->fh.m2m_ctx, buf) {
318 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_IDLE)
325 return call_vop(inst, process_capture, &vbuf->vb2_buf);
328 struct vb2_v4l2_buffer *vpu_next_src_buf(struct vpu_inst *inst)
330 struct vb2_v4l2_buffer *src_buf = NULL;
332 if (!inst->fh.m2m_ctx)
335 src_buf = v4l2_m2m_next_src_buf(inst->fh.m2m_ctx);
336 if (!src_buf || vpu_get_buffer_state(src_buf) == VPU_BUF_STATE_IDLE)
339 while (vpu_vb_is_codecconfig(src_buf)) {
340 v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
341 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
342 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
344 src_buf = v4l2_m2m_next_src_buf(inst->fh.m2m_ctx);
345 if (!src_buf || vpu_get_buffer_state(src_buf) == VPU_BUF_STATE_IDLE)
352 void vpu_skip_frame(struct vpu_inst *inst, int count)
354 struct vb2_v4l2_buffer *src_buf;
355 enum vb2_buffer_state state;
358 if (count <= 0 || !inst->fh.m2m_ctx)
362 src_buf = v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
363 if (!src_buf || vpu_get_buffer_state(src_buf) == VPU_BUF_STATE_IDLE)
365 if (vpu_get_buffer_state(src_buf) == VPU_BUF_STATE_DECODED)
366 state = VB2_BUF_STATE_DONE;
368 state = VB2_BUF_STATE_ERROR;
370 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
371 v4l2_m2m_buf_done(src_buf, state);
375 struct vb2_v4l2_buffer *vpu_find_buf_by_sequence(struct vpu_inst *inst, u32 type, u32 sequence)
377 struct v4l2_m2m_buffer *buf = NULL;
378 struct vb2_v4l2_buffer *vbuf = NULL;
380 if (!inst || !inst->fh.m2m_ctx)
383 if (V4L2_TYPE_IS_OUTPUT(type)) {
384 v4l2_m2m_for_each_src_buf(inst->fh.m2m_ctx, buf) {
386 if (vbuf->sequence == sequence)
391 v4l2_m2m_for_each_dst_buf(inst->fh.m2m_ctx, buf) {
393 if (vbuf->sequence == sequence)
402 struct vb2_v4l2_buffer *vpu_find_buf_by_idx(struct vpu_inst *inst, u32 type, u32 idx)
404 struct v4l2_m2m_buffer *buf = NULL;
405 struct vb2_v4l2_buffer *vbuf = NULL;
407 if (!inst || !inst->fh.m2m_ctx)
410 if (V4L2_TYPE_IS_OUTPUT(type)) {
411 v4l2_m2m_for_each_src_buf(inst->fh.m2m_ctx, buf) {
413 if (vbuf->vb2_buf.index == idx)
418 v4l2_m2m_for_each_dst_buf(inst->fh.m2m_ctx, buf) {
420 if (vbuf->vb2_buf.index == idx)
429 int vpu_get_num_buffers(struct vpu_inst *inst, u32 type)
433 if (!inst || !inst->fh.m2m_ctx)
436 if (V4L2_TYPE_IS_OUTPUT(type))
437 q = v4l2_m2m_get_src_vq(inst->fh.m2m_ctx);
439 q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
441 return q->num_buffers;
444 static void vpu_m2m_device_run(void *priv)
448 static void vpu_m2m_job_abort(void *priv)
450 struct vpu_inst *inst = priv;
451 struct v4l2_m2m_ctx *m2m_ctx = inst->fh.m2m_ctx;
453 v4l2_m2m_job_finish(m2m_ctx->m2m_dev, m2m_ctx);
456 static const struct v4l2_m2m_ops vpu_m2m_ops = {
457 .device_run = vpu_m2m_device_run,
458 .job_abort = vpu_m2m_job_abort
461 static int vpu_vb2_queue_setup(struct vb2_queue *vq,
462 unsigned int *buf_count,
463 unsigned int *plane_count,
464 unsigned int psize[],
465 struct device *allocators[])
467 struct vpu_inst *inst = vb2_get_drv_priv(vq);
468 struct vpu_format *cur_fmt;
471 cur_fmt = vpu_get_format(inst, vq->type);
474 if (*plane_count != cur_fmt->mem_planes)
476 for (i = 0; i < cur_fmt->mem_planes; i++) {
477 if (psize[i] < vpu_get_fmt_plane_size(cur_fmt, i))
483 if (V4L2_TYPE_IS_OUTPUT(vq->type))
484 *buf_count = max_t(unsigned int, *buf_count, inst->min_buffer_out);
486 *buf_count = max_t(unsigned int, *buf_count, inst->min_buffer_cap);
487 *plane_count = cur_fmt->mem_planes;
488 for (i = 0; i < cur_fmt->mem_planes; i++)
489 psize[i] = vpu_get_fmt_plane_size(cur_fmt, i);
494 static int vpu_vb2_buf_init(struct vb2_buffer *vb)
496 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
498 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
502 static int vpu_vb2_buf_out_validate(struct vb2_buffer *vb)
504 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
506 vbuf->field = V4L2_FIELD_NONE;
511 static int vpu_vb2_buf_prepare(struct vb2_buffer *vb)
513 struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
514 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
515 struct vpu_format *cur_fmt;
518 cur_fmt = vpu_get_format(inst, vb->type);
519 for (i = 0; i < cur_fmt->mem_planes; i++) {
520 if (vpu_get_vb_length(vb, i) < vpu_get_fmt_plane_size(cur_fmt, i)) {
521 dev_dbg(inst->dev, "[%d] %s buf[%d] is invalid\n",
522 inst->id, vpu_type_name(vb->type), vb->index);
523 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_ERROR);
530 static void vpu_vb2_buf_finish(struct vb2_buffer *vb)
532 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
533 struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
534 struct vb2_queue *q = vb->vb2_queue;
536 if (vbuf->flags & V4L2_BUF_FLAG_LAST)
537 vpu_notify_eos(inst);
539 if (list_empty(&q->done_list))
540 call_void_vop(inst, on_queue_empty, q->type);
543 void vpu_vb2_buffers_return(struct vpu_inst *inst, unsigned int type, enum vb2_buffer_state state)
545 struct vb2_v4l2_buffer *buf;
547 if (V4L2_TYPE_IS_OUTPUT(type)) {
548 while ((buf = v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx))) {
549 vpu_set_buffer_state(buf, VPU_BUF_STATE_IDLE);
550 v4l2_m2m_buf_done(buf, state);
553 while ((buf = v4l2_m2m_dst_buf_remove(inst->fh.m2m_ctx))) {
554 vpu_set_buffer_state(buf, VPU_BUF_STATE_IDLE);
555 v4l2_m2m_buf_done(buf, state);
560 static int vpu_vb2_start_streaming(struct vb2_queue *q, unsigned int count)
562 struct vpu_inst *inst = vb2_get_drv_priv(q);
563 struct vpu_format *fmt = vpu_get_format(inst, q->type);
566 vpu_inst_unlock(inst);
567 ret = vpu_inst_register(inst);
570 vpu_vb2_buffers_return(inst, q->type, VB2_BUF_STATE_QUEUED);
574 vpu_trace(inst->dev, "[%d] %s %c%c%c%c %dx%d %u(%u) %u(%u) %u(%u) %d\n",
575 inst->id, vpu_type_name(q->type),
580 fmt->width, fmt->height,
581 fmt->sizeimage[0], fmt->bytesperline[0],
582 fmt->sizeimage[1], fmt->bytesperline[1],
583 fmt->sizeimage[2], fmt->bytesperline[2],
585 vb2_clear_last_buffer_dequeued(q);
586 ret = call_vop(inst, start, q->type);
588 vpu_vb2_buffers_return(inst, q->type, VB2_BUF_STATE_QUEUED);
593 static void vpu_vb2_stop_streaming(struct vb2_queue *q)
595 struct vpu_inst *inst = vb2_get_drv_priv(q);
597 vpu_trace(inst->dev, "[%d] %s\n", inst->id, vpu_type_name(q->type));
599 call_void_vop(inst, stop, q->type);
600 vpu_vb2_buffers_return(inst, q->type, VB2_BUF_STATE_ERROR);
601 if (V4L2_TYPE_IS_OUTPUT(q->type))
605 static void vpu_vb2_buf_queue(struct vb2_buffer *vb)
607 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
608 struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
610 if (V4L2_TYPE_IS_OUTPUT(vb->type))
611 vbuf->sequence = inst->sequence++;
613 v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
614 vpu_process_output_buffer(inst);
615 vpu_process_capture_buffer(inst);
618 static const struct vb2_ops vpu_vb2_ops = {
619 .queue_setup = vpu_vb2_queue_setup,
620 .buf_init = vpu_vb2_buf_init,
621 .buf_out_validate = vpu_vb2_buf_out_validate,
622 .buf_prepare = vpu_vb2_buf_prepare,
623 .buf_finish = vpu_vb2_buf_finish,
624 .start_streaming = vpu_vb2_start_streaming,
625 .stop_streaming = vpu_vb2_stop_streaming,
626 .buf_queue = vpu_vb2_buf_queue,
627 .wait_prepare = vb2_ops_wait_prepare,
628 .wait_finish = vb2_ops_wait_finish,
631 static int vpu_m2m_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
633 struct vpu_inst *inst = priv;
636 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
637 inst->out_format.type = src_vq->type;
638 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
639 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
640 src_vq->ops = &vpu_vb2_ops;
641 src_vq->mem_ops = &vb2_dma_contig_memops;
642 if (inst->type == VPU_CORE_TYPE_DEC && inst->use_stream_buffer)
643 src_vq->mem_ops = &vb2_vmalloc_memops;
644 src_vq->drv_priv = inst;
645 src_vq->buf_struct_size = sizeof(struct vpu_vb2_buffer);
646 src_vq->min_buffers_needed = 1;
647 src_vq->dev = inst->vpu->dev;
648 src_vq->lock = &inst->lock;
649 ret = vb2_queue_init(src_vq);
653 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
654 inst->cap_format.type = dst_vq->type;
655 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
656 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
657 dst_vq->ops = &vpu_vb2_ops;
658 dst_vq->mem_ops = &vb2_dma_contig_memops;
659 if (inst->type == VPU_CORE_TYPE_ENC && inst->use_stream_buffer)
660 dst_vq->mem_ops = &vb2_vmalloc_memops;
661 dst_vq->drv_priv = inst;
662 dst_vq->buf_struct_size = sizeof(struct vpu_vb2_buffer);
663 dst_vq->min_buffers_needed = 1;
664 dst_vq->dev = inst->vpu->dev;
665 dst_vq->lock = &inst->lock;
666 ret = vb2_queue_init(dst_vq);
668 vb2_queue_release(src_vq);
675 static int vpu_v4l2_release(struct vpu_inst *inst)
677 vpu_trace(inst->vpu->dev, "%p\n", inst);
679 vpu_release_core(inst->core);
680 put_device(inst->dev);
682 if (inst->workqueue) {
683 cancel_work_sync(&inst->msg_work);
684 destroy_workqueue(inst->workqueue);
685 inst->workqueue = NULL;
688 v4l2_ctrl_handler_free(&inst->ctrl_handler);
689 mutex_destroy(&inst->lock);
690 v4l2_fh_del(&inst->fh);
691 v4l2_fh_exit(&inst->fh);
693 call_void_vop(inst, cleanup);
698 int vpu_v4l2_open(struct file *file, struct vpu_inst *inst)
700 struct vpu_dev *vpu = video_drvdata(file);
701 struct vpu_func *func;
704 if (!inst || !inst->ops)
707 if (inst->type == VPU_CORE_TYPE_ENC)
708 func = &vpu->encoder;
710 func = &vpu->decoder;
712 atomic_set(&inst->ref_count, 0);
715 inst->core = vpu_request_core(vpu, inst->type);
717 inst->dev = get_device(inst->core->dev);
718 mutex_init(&inst->lock);
719 INIT_LIST_HEAD(&inst->cmd_q);
720 inst->id = VPU_INST_NULL_ID;
721 inst->release = vpu_v4l2_release;
722 inst->pid = current->pid;
723 inst->tgid = current->tgid;
724 inst->min_buffer_cap = 2;
725 inst->min_buffer_out = 2;
726 v4l2_fh_init(&inst->fh, func->vfd);
727 v4l2_fh_add(&inst->fh);
729 ret = call_vop(inst, ctrl_init);
733 inst->fh.m2m_ctx = v4l2_m2m_ctx_init(func->m2m_dev, inst, vpu_m2m_queue_init);
734 if (IS_ERR(inst->fh.m2m_ctx)) {
735 dev_err(vpu->dev, "v4l2_m2m_ctx_init fail\n");
736 ret = PTR_ERR(inst->fh.m2m_ctx);
740 inst->fh.ctrl_handler = &inst->ctrl_handler;
741 file->private_data = &inst->fh;
742 inst->state = VPU_CODEC_STATE_DEINIT;
743 inst->workqueue = alloc_workqueue("vpu_inst", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
744 if (inst->workqueue) {
745 INIT_WORK(&inst->msg_work, vpu_inst_run_work);
746 ret = kfifo_init(&inst->msg_fifo,
748 rounddown_pow_of_two(sizeof(inst->msg_buffer)));
750 destroy_workqueue(inst->workqueue);
751 inst->workqueue = NULL;
754 vpu_trace(vpu->dev, "tgid = %d, pid = %d, type = %s, inst = %p\n",
755 inst->tgid, inst->pid, vpu_core_type_desc(inst->type), inst);
763 int vpu_v4l2_close(struct file *file)
765 struct vpu_dev *vpu = video_drvdata(file);
766 struct vpu_inst *inst = to_inst(file);
768 vpu_trace(vpu->dev, "tgid = %d, pid = %d, inst = %p\n", inst->tgid, inst->pid, inst);
771 if (inst->fh.m2m_ctx) {
772 v4l2_m2m_ctx_release(inst->fh.m2m_ctx);
773 inst->fh.m2m_ctx = NULL;
775 vpu_inst_unlock(inst);
777 call_void_vop(inst, release);
778 vpu_inst_unregister(inst);
784 int vpu_add_func(struct vpu_dev *vpu, struct vpu_func *func)
786 struct video_device *vfd;
795 func->m2m_dev = v4l2_m2m_init(&vpu_m2m_ops);
796 if (IS_ERR(func->m2m_dev)) {
797 dev_err(vpu->dev, "v4l2_m2m_init fail\n");
799 return PTR_ERR(func->m2m_dev);
802 vfd = video_device_alloc();
804 v4l2_m2m_release(func->m2m_dev);
805 dev_err(vpu->dev, "alloc vpu decoder video device fail\n");
808 vfd->release = video_device_release;
809 vfd->vfl_dir = VFL_DIR_M2M;
810 vfd->v4l2_dev = &vpu->v4l2_dev;
811 vfd->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
812 if (func->type == VPU_CORE_TYPE_ENC) {
813 strscpy(vfd->name, "amphion-vpu-encoder", sizeof(vfd->name));
814 vfd->fops = venc_get_fops();
815 vfd->ioctl_ops = venc_get_ioctl_ops();
817 strscpy(vfd->name, "amphion-vpu-decoder", sizeof(vfd->name));
818 vfd->fops = vdec_get_fops();
819 vfd->ioctl_ops = vdec_get_ioctl_ops();
822 ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
824 video_device_release(vfd);
825 v4l2_m2m_release(func->m2m_dev);
828 video_set_drvdata(vfd, vpu);
831 ret = v4l2_m2m_register_media_controller(func->m2m_dev, func->vfd, func->function);
833 v4l2_m2m_release(func->m2m_dev);
834 func->m2m_dev = NULL;
835 video_unregister_device(func->vfd);
843 void vpu_remove_func(struct vpu_func *func)
849 v4l2_m2m_unregister_media_controller(func->m2m_dev);
850 v4l2_m2m_release(func->m2m_dev);
851 func->m2m_dev = NULL;
854 video_unregister_device(func->vfd);