xref: /linux/drivers/media/platform/amphion/vpu_v4l2.c (revision 0cdee263bc5e7b20f657ea09f9272f50c568f35b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020-2021 NXP
4  */
5 
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>
21 #include "vpu.h"
22 #include "vpu_core.h"
23 #include "vpu_v4l2.h"
24 #include "vpu_msgs.h"
25 #include "vpu_helpers.h"
26 
vpu_inst_lock(struct vpu_inst * inst)27 void vpu_inst_lock(struct vpu_inst *inst)
28 {
29 	mutex_lock(&inst->lock);
30 }
31 
vpu_inst_unlock(struct vpu_inst * inst)32 void vpu_inst_unlock(struct vpu_inst *inst)
33 {
34 	mutex_unlock(&inst->lock);
35 }
36 
vpu_get_vb_phy_addr(struct vb2_buffer * vb,u32 plane_no)37 dma_addr_t vpu_get_vb_phy_addr(struct vb2_buffer *vb, u32 plane_no)
38 {
39 	if (plane_no >= vb->num_planes)
40 		return 0;
41 	return vb2_dma_contig_plane_dma_addr(vb, plane_no) +
42 			vb->planes[plane_no].data_offset;
43 }
44 
vpu_get_vb_length(struct vb2_buffer * vb,u32 plane_no)45 unsigned int vpu_get_vb_length(struct vb2_buffer *vb, u32 plane_no)
46 {
47 	if (plane_no >= vb->num_planes)
48 		return 0;
49 	return vb2_plane_size(vb, plane_no) - vb->planes[plane_no].data_offset;
50 }
51 
vpu_set_buffer_state(struct vb2_v4l2_buffer * vbuf,unsigned int state)52 void vpu_set_buffer_state(struct vb2_v4l2_buffer *vbuf, unsigned int state)
53 {
54 	struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf);
55 
56 	vpu_buf->state = state;
57 }
58 
vpu_get_buffer_state(struct vb2_v4l2_buffer * vbuf)59 unsigned int vpu_get_buffer_state(struct vb2_v4l2_buffer *vbuf)
60 {
61 	struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf);
62 
63 	return vpu_buf->state;
64 }
65 
vpu_set_buffer_average_qp(struct vb2_v4l2_buffer * vbuf,u32 qp)66 void vpu_set_buffer_average_qp(struct vb2_v4l2_buffer *vbuf, u32 qp)
67 {
68 	struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf);
69 
70 	vpu_buf->average_qp = qp;
71 }
72 
vpu_v4l2_set_error(struct vpu_inst * inst)73 void vpu_v4l2_set_error(struct vpu_inst *inst)
74 {
75 	vpu_inst_lock(inst);
76 	dev_err(inst->dev, "some error occurs in codec\n");
77 	if (inst->fh.m2m_ctx) {
78 		vb2_queue_error(v4l2_m2m_get_src_vq(inst->fh.m2m_ctx));
79 		vb2_queue_error(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx));
80 	}
81 	vpu_inst_unlock(inst);
82 }
83 
vpu_notify_eos(struct vpu_inst * inst)84 int vpu_notify_eos(struct vpu_inst *inst)
85 {
86 	static const struct v4l2_event ev = {
87 		.id = 0,
88 		.type = V4L2_EVENT_EOS
89 	};
90 
91 	vpu_trace(inst->dev, "[%d]\n", inst->id);
92 	v4l2_event_queue_fh(&inst->fh, &ev);
93 
94 	return 0;
95 }
96 
vpu_notify_source_change(struct vpu_inst * inst)97 int vpu_notify_source_change(struct vpu_inst *inst)
98 {
99 	static const struct v4l2_event ev = {
100 		.id = 0,
101 		.type = V4L2_EVENT_SOURCE_CHANGE,
102 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION
103 	};
104 
105 	vpu_trace(inst->dev, "[%d]\n", inst->id);
106 	v4l2_event_queue_fh(&inst->fh, &ev);
107 	return 0;
108 }
109 
vpu_set_last_buffer_dequeued(struct vpu_inst * inst,bool eos)110 int vpu_set_last_buffer_dequeued(struct vpu_inst *inst, bool eos)
111 {
112 	struct vb2_queue *q;
113 
114 	if (!inst || !inst->fh.m2m_ctx)
115 		return -EINVAL;
116 
117 	q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
118 	if (!list_empty(&q->done_list))
119 		return -EINVAL;
120 
121 	if (q->last_buffer_dequeued)
122 		return 0;
123 	vpu_trace(inst->dev, "last buffer dequeued\n");
124 	q->last_buffer_dequeued = true;
125 	wake_up(&q->done_wq);
126 	if (eos)
127 		vpu_notify_eos(inst);
128 	return 0;
129 }
130 
vpu_is_source_empty(struct vpu_inst * inst)131 bool vpu_is_source_empty(struct vpu_inst *inst)
132 {
133 	struct v4l2_m2m_buffer *buf = NULL;
134 
135 	if (!inst->fh.m2m_ctx)
136 		return true;
137 	v4l2_m2m_for_each_src_buf(inst->fh.m2m_ctx, buf) {
138 		if (vpu_get_buffer_state(&buf->vb) == VPU_BUF_STATE_IDLE)
139 			return false;
140 	}
141 	return true;
142 }
143 
vpu_init_format(struct vpu_inst * inst,struct vpu_format * fmt)144 static int vpu_init_format(struct vpu_inst *inst, struct vpu_format *fmt)
145 {
146 	const struct vpu_format *info;
147 
148 	info = vpu_helper_find_format(inst, fmt->type, fmt->pixfmt);
149 	if (!info) {
150 		info = vpu_helper_enum_format(inst, fmt->type, 0);
151 		if (!info)
152 			return -EINVAL;
153 	}
154 	memcpy(fmt, info, sizeof(*fmt));
155 
156 	return 0;
157 }
158 
vpu_calc_fmt_bytesperline(struct v4l2_format * f,struct vpu_format * fmt)159 static int vpu_calc_fmt_bytesperline(struct v4l2_format *f, struct vpu_format *fmt)
160 {
161 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
162 	int i;
163 
164 	if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
165 		for (i = 0; i < fmt->comp_planes; i++)
166 			fmt->bytesperline[i] = 0;
167 		return 0;
168 	}
169 	if (pixmp->num_planes == fmt->comp_planes) {
170 		for (i = 0; i < fmt->comp_planes; i++)
171 			fmt->bytesperline[i] = pixmp->plane_fmt[i].bytesperline;
172 		return 0;
173 	}
174 	if (pixmp->num_planes > 1)
175 		return -EINVAL;
176 
177 	/*amphion vpu only support nv12 and nv12 tiled,
178 	 * so the bytesperline of luma and chroma should be same
179 	 */
180 	for (i = 0; i < fmt->comp_planes; i++)
181 		fmt->bytesperline[i] = pixmp->plane_fmt[0].bytesperline;
182 
183 	return 0;
184 }
185 
vpu_calc_fmt_sizeimage(struct vpu_inst * inst,struct vpu_format * fmt)186 static int vpu_calc_fmt_sizeimage(struct vpu_inst *inst, struct vpu_format *fmt)
187 {
188 	u32 stride = 1;
189 	int i;
190 
191 	if (!(fmt->flags & V4L2_FMT_FLAG_COMPRESSED)) {
192 		const struct vpu_core_resources *res = vpu_get_resource(inst);
193 
194 		if (res)
195 			stride = res->stride;
196 	}
197 
198 	for (i = 0; i < fmt->comp_planes; i++) {
199 		fmt->sizeimage[i] = vpu_helper_get_plane_size(fmt->pixfmt,
200 							      fmt->width,
201 							      fmt->height,
202 							      i,
203 							      stride,
204 							      fmt->field != V4L2_FIELD_NONE ? 1 : 0,
205 							      &fmt->bytesperline[i]);
206 		fmt->sizeimage[i] = max_t(u32, fmt->sizeimage[i], PAGE_SIZE);
207 		if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
208 			fmt->sizeimage[i] = clamp_val(fmt->sizeimage[i], SZ_128K, SZ_8M);
209 			fmt->bytesperline[i] = 0;
210 		}
211 	}
212 
213 	return 0;
214 }
215 
vpu_get_fmt_plane_size(struct vpu_format * fmt,u32 plane_no)216 u32 vpu_get_fmt_plane_size(struct vpu_format *fmt, u32 plane_no)
217 {
218 	u32 size;
219 	int i;
220 
221 	if (plane_no >= fmt->mem_planes)
222 		return 0;
223 
224 	if (fmt->comp_planes == fmt->mem_planes)
225 		return fmt->sizeimage[plane_no];
226 	if (plane_no < fmt->mem_planes - 1)
227 		return fmt->sizeimage[plane_no];
228 
229 	size = fmt->sizeimage[plane_no];
230 	for (i = fmt->mem_planes; i < fmt->comp_planes; i++)
231 		size += fmt->sizeimage[i];
232 
233 	return size;
234 }
235 
vpu_try_fmt_common(struct vpu_inst * inst,struct v4l2_format * f,struct vpu_format * fmt)236 int vpu_try_fmt_common(struct vpu_inst *inst, struct v4l2_format *f, struct vpu_format *fmt)
237 {
238 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
239 	int i;
240 	int ret;
241 
242 	fmt->pixfmt = pixmp->pixelformat;
243 	fmt->type = f->type;
244 	ret = vpu_init_format(inst, fmt);
245 	if (ret < 0)
246 		return ret;
247 
248 	fmt->width = pixmp->width;
249 	fmt->height = pixmp->height;
250 	if (fmt->width)
251 		fmt->width = vpu_helper_valid_frame_width(inst, fmt->width);
252 	if (fmt->height)
253 		fmt->height = vpu_helper_valid_frame_height(inst, fmt->height);
254 	fmt->field = pixmp->field == V4L2_FIELD_ANY ? V4L2_FIELD_NONE : pixmp->field;
255 	vpu_calc_fmt_bytesperline(f, fmt);
256 	vpu_calc_fmt_sizeimage(inst, fmt);
257 	if ((fmt->flags & V4L2_FMT_FLAG_COMPRESSED) && pixmp->plane_fmt[0].sizeimage)
258 		fmt->sizeimage[0] = clamp_val(pixmp->plane_fmt[0].sizeimage, SZ_128K, SZ_8M);
259 
260 	pixmp->pixelformat = fmt->pixfmt;
261 	pixmp->width = fmt->width;
262 	pixmp->height = fmt->height;
263 	pixmp->flags = fmt->flags;
264 	pixmp->num_planes = fmt->mem_planes;
265 	pixmp->field = fmt->field;
266 	memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
267 	for (i = 0; i < pixmp->num_planes; i++) {
268 		pixmp->plane_fmt[i].bytesperline = fmt->bytesperline[i];
269 		pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(fmt, i);
270 		memset(pixmp->plane_fmt[i].reserved, 0, sizeof(pixmp->plane_fmt[i].reserved));
271 	}
272 
273 	return 0;
274 }
275 
vpu_check_ready(struct vpu_inst * inst,u32 type)276 static bool vpu_check_ready(struct vpu_inst *inst, u32 type)
277 {
278 	if (!inst)
279 		return false;
280 	if (inst->state == VPU_CODEC_STATE_DEINIT || inst->id < 0)
281 		return false;
282 	if (!inst->ops->check_ready)
283 		return true;
284 	return call_vop(inst, check_ready, type);
285 }
286 
vpu_process_output_buffer(struct vpu_inst * inst)287 int vpu_process_output_buffer(struct vpu_inst *inst)
288 {
289 	struct v4l2_m2m_buffer *buf = NULL;
290 	struct vb2_v4l2_buffer *vbuf = NULL;
291 
292 	if (!inst || !inst->fh.m2m_ctx)
293 		return -EINVAL;
294 
295 	if (!vpu_check_ready(inst, inst->out_format.type))
296 		return -EINVAL;
297 
298 	v4l2_m2m_for_each_src_buf(inst->fh.m2m_ctx, buf) {
299 		vbuf = &buf->vb;
300 		if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_IDLE)
301 			break;
302 		vbuf = NULL;
303 	}
304 
305 	if (!vbuf)
306 		return -EINVAL;
307 
308 	dev_dbg(inst->dev, "[%d]frame id = %d / %d\n",
309 		inst->id, vbuf->sequence, inst->sequence);
310 	return call_vop(inst, process_output, &vbuf->vb2_buf);
311 }
312 
vpu_process_capture_buffer(struct vpu_inst * inst)313 int vpu_process_capture_buffer(struct vpu_inst *inst)
314 {
315 	struct v4l2_m2m_buffer *buf = NULL;
316 	struct vb2_v4l2_buffer *vbuf = NULL;
317 
318 	if (!inst || !inst->fh.m2m_ctx)
319 		return -EINVAL;
320 
321 	if (!vpu_check_ready(inst, inst->cap_format.type))
322 		return -EINVAL;
323 
324 	v4l2_m2m_for_each_dst_buf(inst->fh.m2m_ctx, buf) {
325 		vbuf = &buf->vb;
326 		if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_IDLE)
327 			break;
328 		vbuf = NULL;
329 	}
330 	if (!vbuf)
331 		return -EINVAL;
332 
333 	return call_vop(inst, process_capture, &vbuf->vb2_buf);
334 }
335 
vpu_next_src_buf(struct vpu_inst * inst)336 struct vb2_v4l2_buffer *vpu_next_src_buf(struct vpu_inst *inst)
337 {
338 	struct vb2_v4l2_buffer *src_buf = NULL;
339 
340 	if (!inst->fh.m2m_ctx)
341 		return NULL;
342 
343 	src_buf = v4l2_m2m_next_src_buf(inst->fh.m2m_ctx);
344 	if (!src_buf || vpu_get_buffer_state(src_buf) == VPU_BUF_STATE_IDLE)
345 		return NULL;
346 
347 	while (vpu_vb_is_codecconfig(src_buf)) {
348 		v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
349 		vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
350 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
351 
352 		src_buf = v4l2_m2m_next_src_buf(inst->fh.m2m_ctx);
353 		if (!src_buf || vpu_get_buffer_state(src_buf) == VPU_BUF_STATE_IDLE)
354 			return NULL;
355 	}
356 
357 	return src_buf;
358 }
359 
vpu_skip_frame(struct vpu_inst * inst,int count)360 void vpu_skip_frame(struct vpu_inst *inst, int count)
361 {
362 	struct vb2_v4l2_buffer *src_buf;
363 	enum vb2_buffer_state state;
364 	int i = 0;
365 
366 	if (count <= 0 || !inst->fh.m2m_ctx)
367 		return;
368 
369 	while (i < count) {
370 		src_buf = v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
371 		if (!src_buf || vpu_get_buffer_state(src_buf) == VPU_BUF_STATE_IDLE)
372 			return;
373 		if (vpu_get_buffer_state(src_buf) == VPU_BUF_STATE_DECODED)
374 			state = VB2_BUF_STATE_DONE;
375 		else
376 			state = VB2_BUF_STATE_ERROR;
377 		i++;
378 		vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
379 		v4l2_m2m_buf_done(src_buf, state);
380 	}
381 }
382 
vpu_find_buf_by_sequence(struct vpu_inst * inst,u32 type,u32 sequence)383 struct vb2_v4l2_buffer *vpu_find_buf_by_sequence(struct vpu_inst *inst, u32 type, u32 sequence)
384 {
385 	struct v4l2_m2m_buffer *buf = NULL;
386 	struct vb2_v4l2_buffer *vbuf = NULL;
387 
388 	if (!inst || !inst->fh.m2m_ctx)
389 		return NULL;
390 
391 	if (V4L2_TYPE_IS_OUTPUT(type)) {
392 		v4l2_m2m_for_each_src_buf(inst->fh.m2m_ctx, buf) {
393 			vbuf = &buf->vb;
394 			if (vbuf->sequence == sequence)
395 				break;
396 			vbuf = NULL;
397 		}
398 	} else {
399 		v4l2_m2m_for_each_dst_buf(inst->fh.m2m_ctx, buf) {
400 			vbuf = &buf->vb;
401 			if (vbuf->sequence == sequence)
402 				break;
403 			vbuf = NULL;
404 		}
405 	}
406 
407 	return vbuf;
408 }
409 
vpu_find_buf_by_idx(struct vpu_inst * inst,u32 type,u32 idx)410 struct vb2_v4l2_buffer *vpu_find_buf_by_idx(struct vpu_inst *inst, u32 type, u32 idx)
411 {
412 	struct v4l2_m2m_buffer *buf = NULL;
413 	struct vb2_v4l2_buffer *vbuf = NULL;
414 
415 	if (!inst || !inst->fh.m2m_ctx)
416 		return NULL;
417 
418 	if (V4L2_TYPE_IS_OUTPUT(type)) {
419 		v4l2_m2m_for_each_src_buf(inst->fh.m2m_ctx, buf) {
420 			vbuf = &buf->vb;
421 			if (vbuf->vb2_buf.index == idx)
422 				break;
423 			vbuf = NULL;
424 		}
425 	} else {
426 		v4l2_m2m_for_each_dst_buf(inst->fh.m2m_ctx, buf) {
427 			vbuf = &buf->vb;
428 			if (vbuf->vb2_buf.index == idx)
429 				break;
430 			vbuf = NULL;
431 		}
432 	}
433 
434 	return vbuf;
435 }
436 
vpu_get_num_buffers(struct vpu_inst * inst,u32 type)437 int vpu_get_num_buffers(struct vpu_inst *inst, u32 type)
438 {
439 	struct vb2_queue *q;
440 
441 	if (!inst || !inst->fh.m2m_ctx)
442 		return -EINVAL;
443 
444 	if (V4L2_TYPE_IS_OUTPUT(type))
445 		q = v4l2_m2m_get_src_vq(inst->fh.m2m_ctx);
446 	else
447 		q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
448 
449 	return vb2_get_num_buffers(q);
450 }
451 
vpu_m2m_device_run(void * priv)452 static void vpu_m2m_device_run(void *priv)
453 {
454 }
455 
vpu_m2m_job_abort(void * priv)456 static void vpu_m2m_job_abort(void *priv)
457 {
458 	struct vpu_inst *inst = priv;
459 	struct v4l2_m2m_ctx *m2m_ctx = inst->fh.m2m_ctx;
460 
461 	v4l2_m2m_job_finish(m2m_ctx->m2m_dev, m2m_ctx);
462 }
463 
464 static const struct v4l2_m2m_ops vpu_m2m_ops = {
465 	.device_run = vpu_m2m_device_run,
466 	.job_abort = vpu_m2m_job_abort
467 };
468 
vpu_vb2_queue_setup(struct vb2_queue * vq,unsigned int * buf_count,unsigned int * plane_count,unsigned int psize[],struct device * allocators[])469 static int vpu_vb2_queue_setup(struct vb2_queue *vq,
470 			       unsigned int *buf_count,
471 			       unsigned int *plane_count,
472 			       unsigned int psize[],
473 			       struct device *allocators[])
474 {
475 	struct vpu_inst *inst = vb2_get_drv_priv(vq);
476 	struct vpu_format *cur_fmt;
477 	int i;
478 
479 	cur_fmt = vpu_get_format(inst, vq->type);
480 
481 	if (*plane_count) {
482 		if (*plane_count != cur_fmt->mem_planes)
483 			return -EINVAL;
484 		for (i = 0; i < cur_fmt->mem_planes; i++) {
485 			if (psize[i] < vpu_get_fmt_plane_size(cur_fmt, i))
486 				return -EINVAL;
487 		}
488 		return 0;
489 	}
490 
491 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
492 		*buf_count = max_t(unsigned int, *buf_count, inst->min_buffer_out);
493 	else
494 		*buf_count = max_t(unsigned int, *buf_count, inst->min_buffer_cap);
495 	*plane_count = cur_fmt->mem_planes;
496 	for (i = 0; i < cur_fmt->mem_planes; i++)
497 		psize[i] = vpu_get_fmt_plane_size(cur_fmt, i);
498 
499 	if (V4L2_TYPE_IS_OUTPUT(vq->type) && inst->state == VPU_CODEC_STATE_SEEK) {
500 		vpu_trace(inst->dev, "reinit when VIDIOC_REQBUFS(OUTPUT, 0)\n");
501 		call_void_vop(inst, release);
502 	}
503 
504 	if (V4L2_TYPE_IS_CAPTURE(vq->type))
505 		call_void_vop(inst, reset_frame_store);
506 
507 	return 0;
508 }
509 
vpu_vb2_buf_init(struct vb2_buffer * vb)510 static int vpu_vb2_buf_init(struct vb2_buffer *vb)
511 {
512 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
513 	struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf);
514 	struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
515 
516 	vpu_buf->fs_id = -1;
517 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
518 
519 	if (!inst->ops->attach_frame_store || V4L2_TYPE_IS_OUTPUT(vb->type))
520 		return 0;
521 
522 	call_void_vop(inst, attach_frame_store, vb);
523 	return 0;
524 }
525 
vpu_vb2_buf_out_validate(struct vb2_buffer * vb)526 static int vpu_vb2_buf_out_validate(struct vb2_buffer *vb)
527 {
528 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
529 
530 	vbuf->field = V4L2_FIELD_NONE;
531 
532 	return 0;
533 }
534 
vpu_vb2_buf_prepare(struct vb2_buffer * vb)535 static int vpu_vb2_buf_prepare(struct vb2_buffer *vb)
536 {
537 	struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
538 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
539 	struct vpu_format *cur_fmt;
540 	u32 i;
541 
542 	cur_fmt = vpu_get_format(inst, vb->type);
543 	for (i = 0; i < cur_fmt->mem_planes; i++) {
544 		if (vpu_get_vb_length(vb, i) < vpu_get_fmt_plane_size(cur_fmt, i)) {
545 			dev_dbg(inst->dev, "[%d] %s buf[%d] is invalid\n",
546 				inst->id, vpu_type_name(vb->type), vb->index);
547 			vpu_set_buffer_state(vbuf, VPU_BUF_STATE_ERROR);
548 		}
549 	}
550 
551 	return 0;
552 }
553 
vpu_vb2_buf_finish(struct vb2_buffer * vb)554 static void vpu_vb2_buf_finish(struct vb2_buffer *vb)
555 {
556 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
557 	struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
558 	struct vb2_queue *q = vb->vb2_queue;
559 
560 	if (V4L2_TYPE_IS_CAPTURE(vb->type)) {
561 		struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf);
562 		struct v4l2_ctrl *ctrl = v4l2_ctrl_find(&inst->ctrl_handler,
563 							V4L2_CID_MPEG_VIDEO_AVERAGE_QP);
564 
565 		if (ctrl)
566 			v4l2_ctrl_s_ctrl(ctrl, vpu_buf->average_qp);
567 	}
568 
569 	if (vbuf->flags & V4L2_BUF_FLAG_LAST)
570 		vpu_notify_eos(inst);
571 
572 	if (list_empty(&q->done_list))
573 		call_void_vop(inst, on_queue_empty, q->type);
574 }
575 
vpu_vb2_buffers_return(struct vpu_inst * inst,unsigned int type,enum vb2_buffer_state state)576 void vpu_vb2_buffers_return(struct vpu_inst *inst, unsigned int type, enum vb2_buffer_state state)
577 {
578 	struct vb2_v4l2_buffer *buf;
579 
580 	if (V4L2_TYPE_IS_OUTPUT(type)) {
581 		while ((buf = v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx))) {
582 			vpu_set_buffer_state(buf, VPU_BUF_STATE_IDLE);
583 			v4l2_m2m_buf_done(buf, state);
584 		}
585 	} else {
586 		while ((buf = v4l2_m2m_dst_buf_remove(inst->fh.m2m_ctx))) {
587 			vpu_set_buffer_state(buf, VPU_BUF_STATE_IDLE);
588 			v4l2_m2m_buf_done(buf, state);
589 		}
590 	}
591 }
592 
vpu_vb2_start_streaming(struct vb2_queue * q,unsigned int count)593 static int vpu_vb2_start_streaming(struct vb2_queue *q, unsigned int count)
594 {
595 	struct vpu_inst *inst = vb2_get_drv_priv(q);
596 	struct vpu_format *fmt = vpu_get_format(inst, q->type);
597 	int ret;
598 
599 	vpu_inst_unlock(inst);
600 	ret = vpu_inst_register(inst);
601 	vpu_inst_lock(inst);
602 	if (ret) {
603 		vpu_vb2_buffers_return(inst, q->type, VB2_BUF_STATE_QUEUED);
604 		return ret;
605 	}
606 
607 	vpu_trace(inst->dev, "[%d] %s %c%c%c%c %dx%d %u(%u) %u(%u) %u(%u) %d\n",
608 		  inst->id, vpu_type_name(q->type),
609 		  fmt->pixfmt,
610 		  fmt->pixfmt >> 8,
611 		  fmt->pixfmt >> 16,
612 		  fmt->pixfmt >> 24,
613 		  fmt->width, fmt->height,
614 		  fmt->sizeimage[0], fmt->bytesperline[0],
615 		  fmt->sizeimage[1], fmt->bytesperline[1],
616 		  fmt->sizeimage[2], fmt->bytesperline[2],
617 		  vb2_get_num_buffers(q));
618 	vb2_clear_last_buffer_dequeued(q);
619 	ret = call_vop(inst, start, q->type);
620 	if (ret)
621 		vpu_vb2_buffers_return(inst, q->type, VB2_BUF_STATE_QUEUED);
622 
623 	return ret;
624 }
625 
vpu_vb2_stop_streaming(struct vb2_queue * q)626 static void vpu_vb2_stop_streaming(struct vb2_queue *q)
627 {
628 	struct vpu_inst *inst = vb2_get_drv_priv(q);
629 
630 	vpu_trace(inst->dev, "[%d] %s\n", inst->id, vpu_type_name(q->type));
631 
632 	call_void_vop(inst, stop, q->type);
633 	vpu_vb2_buffers_return(inst, q->type, VB2_BUF_STATE_ERROR);
634 	if (V4L2_TYPE_IS_OUTPUT(q->type))
635 		inst->sequence = 0;
636 }
637 
vpu_vb2_buf_queue(struct vb2_buffer * vb)638 static void vpu_vb2_buf_queue(struct vb2_buffer *vb)
639 {
640 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
641 	struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
642 
643 	if (V4L2_TYPE_IS_OUTPUT(vb->type))
644 		vbuf->sequence = inst->sequence++;
645 
646 	v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
647 	vpu_process_output_buffer(inst);
648 	vpu_process_capture_buffer(inst);
649 }
650 
651 static const struct vb2_ops vpu_vb2_ops = {
652 	.queue_setup        = vpu_vb2_queue_setup,
653 	.buf_init           = vpu_vb2_buf_init,
654 	.buf_out_validate   = vpu_vb2_buf_out_validate,
655 	.buf_prepare        = vpu_vb2_buf_prepare,
656 	.buf_finish         = vpu_vb2_buf_finish,
657 	.start_streaming    = vpu_vb2_start_streaming,
658 	.stop_streaming     = vpu_vb2_stop_streaming,
659 	.buf_queue          = vpu_vb2_buf_queue,
660 };
661 
vpu_m2m_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)662 static int vpu_m2m_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
663 {
664 	struct vpu_inst *inst = priv;
665 	int ret;
666 
667 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
668 	inst->out_format.type = src_vq->type;
669 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
670 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
671 	src_vq->ops = &vpu_vb2_ops;
672 	src_vq->mem_ops = &vb2_dma_contig_memops;
673 	if (inst->type == VPU_CORE_TYPE_DEC && inst->use_stream_buffer)
674 		src_vq->mem_ops = &vb2_vmalloc_memops;
675 	src_vq->drv_priv = inst;
676 	src_vq->buf_struct_size = sizeof(struct vpu_vb2_buffer);
677 	src_vq->min_queued_buffers = 1;
678 	src_vq->dev = inst->vpu->dev;
679 	src_vq->lock = &inst->lock;
680 	ret = vb2_queue_init(src_vq);
681 	if (ret)
682 		return ret;
683 
684 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
685 	inst->cap_format.type = dst_vq->type;
686 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
687 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
688 	dst_vq->ops = &vpu_vb2_ops;
689 	dst_vq->mem_ops = &vb2_dma_contig_memops;
690 	if (inst->type == VPU_CORE_TYPE_ENC && inst->use_stream_buffer)
691 		dst_vq->mem_ops = &vb2_vmalloc_memops;
692 	dst_vq->drv_priv = inst;
693 	dst_vq->buf_struct_size = sizeof(struct vpu_vb2_buffer);
694 	dst_vq->min_queued_buffers = 1;
695 	dst_vq->dev = inst->vpu->dev;
696 	dst_vq->lock = &inst->lock;
697 	ret = vb2_queue_init(dst_vq);
698 	if (ret) {
699 		vb2_queue_release(src_vq);
700 		return ret;
701 	}
702 
703 	return 0;
704 }
705 
vpu_v4l2_release(struct vpu_inst * inst)706 static int vpu_v4l2_release(struct vpu_inst *inst)
707 {
708 	vpu_trace(inst->vpu->dev, "%p\n", inst);
709 
710 	vpu_release_core(inst->core);
711 	put_device(inst->dev);
712 
713 	if (inst->workqueue) {
714 		cancel_work_sync(&inst->msg_work);
715 		destroy_workqueue(inst->workqueue);
716 		inst->workqueue = NULL;
717 	}
718 
719 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
720 	mutex_destroy(&inst->lock);
721 	v4l2_fh_del(&inst->fh);
722 	v4l2_fh_exit(&inst->fh);
723 
724 	call_void_vop(inst, cleanup);
725 
726 	return 0;
727 }
728 
vpu_v4l2_open(struct file * file,struct vpu_inst * inst)729 int vpu_v4l2_open(struct file *file, struct vpu_inst *inst)
730 {
731 	struct vpu_dev *vpu = video_drvdata(file);
732 	struct vpu_func *func;
733 	int ret = 0;
734 
735 	if (!inst || !inst->ops)
736 		return -EINVAL;
737 
738 	if (inst->type == VPU_CORE_TYPE_ENC)
739 		func = &vpu->encoder;
740 	else
741 		func = &vpu->decoder;
742 
743 	atomic_set(&inst->ref_count, 0);
744 	atomic_long_set(&inst->last_response_cmd, 0);
745 	vpu_inst_get(inst);
746 	inst->vpu = vpu;
747 	inst->core = vpu_request_core(vpu, inst->type);
748 	if (inst->core)
749 		inst->dev = get_device(inst->core->dev);
750 	mutex_init(&inst->lock);
751 	INIT_LIST_HEAD(&inst->cmd_q);
752 	inst->id = VPU_INST_NULL_ID;
753 	inst->release = vpu_v4l2_release;
754 	inst->pid = current->pid;
755 	inst->tgid = current->tgid;
756 	inst->min_buffer_cap = 2;
757 	inst->min_buffer_out = 2;
758 	v4l2_fh_init(&inst->fh, func->vfd);
759 	v4l2_fh_add(&inst->fh);
760 
761 	ret = call_vop(inst, ctrl_init);
762 	if (ret)
763 		goto error;
764 
765 	inst->fh.m2m_ctx = v4l2_m2m_ctx_init(func->m2m_dev, inst, vpu_m2m_queue_init);
766 	if (IS_ERR(inst->fh.m2m_ctx)) {
767 		dev_err(vpu->dev, "v4l2_m2m_ctx_init fail\n");
768 		ret = PTR_ERR(inst->fh.m2m_ctx);
769 		goto error;
770 	}
771 
772 	inst->fh.ctrl_handler = &inst->ctrl_handler;
773 	file->private_data = &inst->fh;
774 	inst->state = VPU_CODEC_STATE_DEINIT;
775 	inst->workqueue = alloc_ordered_workqueue("vpu_inst", WQ_MEM_RECLAIM);
776 	if (inst->workqueue) {
777 		INIT_WORK(&inst->msg_work, vpu_inst_run_work);
778 		ret = kfifo_init(&inst->msg_fifo,
779 				 inst->msg_buffer,
780 				 rounddown_pow_of_two(sizeof(inst->msg_buffer)));
781 		if (ret) {
782 			destroy_workqueue(inst->workqueue);
783 			inst->workqueue = NULL;
784 		}
785 	}
786 	vpu_trace(vpu->dev, "tgid = %d, pid = %d, type = %s, inst = %p\n",
787 		  inst->tgid, inst->pid, vpu_core_type_desc(inst->type), inst);
788 
789 	return 0;
790 error:
791 	vpu_inst_put(inst);
792 	return ret;
793 }
794 
vpu_v4l2_close(struct file * file)795 int vpu_v4l2_close(struct file *file)
796 {
797 	struct vpu_dev *vpu = video_drvdata(file);
798 	struct vpu_inst *inst = to_inst(file);
799 
800 	vpu_trace(vpu->dev, "tgid = %d, pid = %d, inst = %p\n", inst->tgid, inst->pid, inst);
801 
802 	vpu_inst_lock(inst);
803 	if (inst->fh.m2m_ctx) {
804 		v4l2_m2m_ctx_release(inst->fh.m2m_ctx);
805 		inst->fh.m2m_ctx = NULL;
806 	}
807 	call_void_vop(inst, release);
808 	vpu_inst_unlock(inst);
809 
810 	vpu_inst_unregister(inst);
811 	vpu_inst_put(inst);
812 
813 	return 0;
814 }
815 
vpu_add_func(struct vpu_dev * vpu,struct vpu_func * func)816 int vpu_add_func(struct vpu_dev *vpu, struct vpu_func *func)
817 {
818 	struct video_device *vfd;
819 	int ret;
820 
821 	if (!vpu || !func)
822 		return -EINVAL;
823 
824 	if (func->vfd)
825 		return 0;
826 
827 	func->m2m_dev = v4l2_m2m_init(&vpu_m2m_ops);
828 	if (IS_ERR(func->m2m_dev)) {
829 		dev_err(vpu->dev, "v4l2_m2m_init fail\n");
830 		func->vfd = NULL;
831 		return PTR_ERR(func->m2m_dev);
832 	}
833 
834 	vfd = video_device_alloc();
835 	if (!vfd) {
836 		v4l2_m2m_release(func->m2m_dev);
837 		dev_err(vpu->dev, "alloc vpu decoder video device fail\n");
838 		return -ENOMEM;
839 	}
840 	vfd->release = video_device_release;
841 	vfd->vfl_dir = VFL_DIR_M2M;
842 	vfd->v4l2_dev = &vpu->v4l2_dev;
843 	vfd->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
844 	if (func->type == VPU_CORE_TYPE_ENC) {
845 		strscpy(vfd->name, "amphion-vpu-encoder", sizeof(vfd->name));
846 		vfd->fops = venc_get_fops();
847 		vfd->ioctl_ops = venc_get_ioctl_ops();
848 	} else {
849 		strscpy(vfd->name, "amphion-vpu-decoder", sizeof(vfd->name));
850 		vfd->fops = vdec_get_fops();
851 		vfd->ioctl_ops = vdec_get_ioctl_ops();
852 	}
853 	video_set_drvdata(vfd, vpu);
854 
855 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
856 	if (ret) {
857 		video_device_release(vfd);
858 		v4l2_m2m_release(func->m2m_dev);
859 		return ret;
860 	}
861 	func->vfd = vfd;
862 
863 	ret = v4l2_m2m_register_media_controller(func->m2m_dev, func->vfd, func->function);
864 	if (ret) {
865 		v4l2_m2m_release(func->m2m_dev);
866 		func->m2m_dev = NULL;
867 		video_unregister_device(func->vfd);
868 		func->vfd = NULL;
869 		return ret;
870 	}
871 
872 	return 0;
873 }
874 
vpu_remove_func(struct vpu_func * func)875 void vpu_remove_func(struct vpu_func *func)
876 {
877 	if (!func)
878 		return;
879 
880 	if (func->m2m_dev) {
881 		v4l2_m2m_unregister_media_controller(func->m2m_dev);
882 		v4l2_m2m_release(func->m2m_dev);
883 		func->m2m_dev = NULL;
884 	}
885 	if (func->vfd) {
886 		video_unregister_device(func->vfd);
887 		func->vfd = NULL;
888 	}
889 }
890