xref: /linux/drivers/media/platform/st/sti/hva/hva-v4l2.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STMicroelectronics SA 2015
4  * Authors: Yannick Fertre <yannick.fertre@st.com>
5  *          Hugues Fruchet <hugues.fruchet@st.com>
6  */
7 
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <media/v4l2-event.h>
12 #include <media/v4l2-ioctl.h>
13 #include <media/videobuf2-dma-contig.h>
14 
15 #include "hva.h"
16 #include "hva-hw.h"
17 
18 #define MIN_FRAMES	1
19 #define MIN_STREAMS	1
20 
21 #define HVA_MIN_WIDTH	32
22 #define HVA_MAX_WIDTH	1920
23 #define HVA_MIN_HEIGHT	32
24 #define HVA_MAX_HEIGHT	1920
25 
26 /* HVA requires a 16x16 pixels alignment for frames */
27 #define HVA_WIDTH_ALIGNMENT	16
28 #define HVA_HEIGHT_ALIGNMENT	16
29 
30 #define HVA_DEFAULT_WIDTH	HVA_MIN_WIDTH
31 #define	HVA_DEFAULT_HEIGHT	HVA_MIN_HEIGHT
32 #define HVA_DEFAULT_FRAME_NUM	1
33 #define HVA_DEFAULT_FRAME_DEN	30
34 
35 #define to_type_str(type) (type == V4L2_BUF_TYPE_VIDEO_OUTPUT ? \
36 			   "frame" : "stream")
37 
file_to_ctx(struct file * filp)38 static inline struct hva_ctx *file_to_ctx(struct file *filp)
39 {
40 	return container_of(file_to_v4l2_fh(filp), struct hva_ctx, fh);
41 }
42 
43 /* registry of available encoders */
44 static const struct hva_enc *hva_encoders[] = {
45 	&nv12h264enc,
46 	&nv21h264enc,
47 };
48 
frame_size(u32 w,u32 h,u32 fmt)49 static inline int frame_size(u32 w, u32 h, u32 fmt)
50 {
51 	switch (fmt) {
52 	case V4L2_PIX_FMT_NV12:
53 	case V4L2_PIX_FMT_NV21:
54 		return (w * h * 3) / 2;
55 	default:
56 		return 0;
57 	}
58 }
59 
frame_stride(u32 w,u32 fmt)60 static inline int frame_stride(u32 w, u32 fmt)
61 {
62 	switch (fmt) {
63 	case V4L2_PIX_FMT_NV12:
64 	case V4L2_PIX_FMT_NV21:
65 		return w;
66 	default:
67 		return 0;
68 	}
69 }
70 
frame_alignment(u32 fmt)71 static inline int frame_alignment(u32 fmt)
72 {
73 	switch (fmt) {
74 	case V4L2_PIX_FMT_NV12:
75 	case V4L2_PIX_FMT_NV21:
76 		/* multiple of 2 */
77 		return 2;
78 	default:
79 		return 1;
80 	}
81 }
82 
estimated_stream_size(u32 w,u32 h)83 static inline int estimated_stream_size(u32 w, u32 h)
84 {
85 	/*
86 	 * HVA only encodes in YUV420 format, whatever the frame format.
87 	 * A compression ratio of 2 is assumed: thus, the maximum size
88 	 * of a stream is estimated to ((width x height x 3 / 2) / 2)
89 	 */
90 	return (w * h * 3) / 4;
91 }
92 
set_default_params(struct hva_ctx * ctx)93 static void set_default_params(struct hva_ctx *ctx)
94 {
95 	struct hva_frameinfo *frameinfo = &ctx->frameinfo;
96 	struct hva_streaminfo *streaminfo = &ctx->streaminfo;
97 
98 	frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
99 	frameinfo->width = HVA_DEFAULT_WIDTH;
100 	frameinfo->height = HVA_DEFAULT_HEIGHT;
101 	frameinfo->aligned_width = ALIGN(frameinfo->width,
102 					 HVA_WIDTH_ALIGNMENT);
103 	frameinfo->aligned_height = ALIGN(frameinfo->height,
104 					  HVA_HEIGHT_ALIGNMENT);
105 	frameinfo->size = frame_size(frameinfo->aligned_width,
106 				     frameinfo->aligned_height,
107 				     frameinfo->pixelformat);
108 
109 	streaminfo->streamformat = V4L2_PIX_FMT_H264;
110 	streaminfo->width = HVA_DEFAULT_WIDTH;
111 	streaminfo->height = HVA_DEFAULT_HEIGHT;
112 
113 	ctx->colorspace = V4L2_COLORSPACE_REC709;
114 	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
115 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
116 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
117 
118 	ctx->max_stream_size = estimated_stream_size(streaminfo->width,
119 						     streaminfo->height);
120 }
121 
hva_find_encoder(struct hva_ctx * ctx,u32 pixelformat,u32 streamformat)122 static const struct hva_enc *hva_find_encoder(struct hva_ctx *ctx,
123 					      u32 pixelformat,
124 					      u32 streamformat)
125 {
126 	struct hva_dev *hva = ctx_to_hdev(ctx);
127 	const struct hva_enc *enc;
128 	unsigned int i;
129 
130 	for (i = 0; i < hva->nb_of_encoders; i++) {
131 		enc = hva->encoders[i];
132 		if ((enc->pixelformat == pixelformat) &&
133 		    (enc->streamformat == streamformat))
134 			return enc;
135 	}
136 
137 	return NULL;
138 }
139 
register_format(u32 format,u32 formats[],u32 * nb_of_formats)140 static void register_format(u32 format, u32 formats[], u32 *nb_of_formats)
141 {
142 	u32 i;
143 	bool found = false;
144 
145 	for (i = 0; i < *nb_of_formats; i++) {
146 		if (format == formats[i]) {
147 			found = true;
148 			break;
149 		}
150 	}
151 
152 	if (!found)
153 		formats[(*nb_of_formats)++] = format;
154 }
155 
register_formats(struct hva_dev * hva)156 static void register_formats(struct hva_dev *hva)
157 {
158 	unsigned int i;
159 
160 	for (i = 0; i < hva->nb_of_encoders; i++) {
161 		register_format(hva->encoders[i]->pixelformat,
162 				hva->pixelformats,
163 				&hva->nb_of_pixelformats);
164 
165 		register_format(hva->encoders[i]->streamformat,
166 				hva->streamformats,
167 				&hva->nb_of_streamformats);
168 	}
169 }
170 
register_encoders(struct hva_dev * hva)171 static void register_encoders(struct hva_dev *hva)
172 {
173 	struct device *dev = hva_to_dev(hva);
174 	unsigned int i;
175 
176 	for (i = 0; i < ARRAY_SIZE(hva_encoders); i++) {
177 		if (hva->nb_of_encoders >= HVA_MAX_ENCODERS) {
178 			dev_dbg(dev,
179 				"%s failed to register %s encoder (%d maximum reached)\n",
180 				HVA_PREFIX, hva_encoders[i]->name,
181 				HVA_MAX_ENCODERS);
182 			return;
183 		}
184 
185 		hva->encoders[hva->nb_of_encoders++] = hva_encoders[i];
186 		dev_info(dev, "%s %s encoder registered\n", HVA_PREFIX,
187 			 hva_encoders[i]->name);
188 	}
189 }
190 
hva_open_encoder(struct hva_ctx * ctx,u32 streamformat,u32 pixelformat,struct hva_enc ** penc)191 static int hva_open_encoder(struct hva_ctx *ctx, u32 streamformat,
192 			    u32 pixelformat, struct hva_enc **penc)
193 {
194 	struct hva_dev *hva = ctx_to_hdev(ctx);
195 	struct device *dev = ctx_to_dev(ctx);
196 	struct hva_enc *enc;
197 	int ret;
198 
199 	/* find an encoder which can deal with these formats */
200 	enc = (struct hva_enc *)hva_find_encoder(ctx, pixelformat,
201 						 streamformat);
202 	if (!enc) {
203 		dev_err(dev, "%s no encoder found matching %4.4s => %4.4s\n",
204 			ctx->name, (char *)&pixelformat, (char *)&streamformat);
205 		return -EINVAL;
206 	}
207 
208 	dev_dbg(dev, "%s one encoder matching %4.4s => %4.4s\n",
209 		ctx->name, (char *)&pixelformat, (char *)&streamformat);
210 
211 	/* update instance name */
212 	snprintf(ctx->name, sizeof(ctx->name), "[%3d:%4.4s]",
213 		 hva->instance_id, (char *)&streamformat);
214 
215 	/* open encoder instance */
216 	ret = enc->open(ctx);
217 	if (ret) {
218 		dev_err(dev, "%s failed to open encoder instance (%d)\n",
219 			ctx->name, ret);
220 		return ret;
221 	}
222 
223 	dev_dbg(dev, "%s %s encoder opened\n", ctx->name, enc->name);
224 
225 	*penc = enc;
226 
227 	return ret;
228 }
229 
hva_dbg_summary(struct hva_ctx * ctx)230 static void hva_dbg_summary(struct hva_ctx *ctx)
231 {
232 	struct device *dev = ctx_to_dev(ctx);
233 	struct hva_streaminfo *stream = &ctx->streaminfo;
234 	struct hva_frameinfo *frame = &ctx->frameinfo;
235 
236 	if (!(ctx->flags & HVA_FLAG_STREAMINFO))
237 		return;
238 
239 	dev_dbg(dev, "%s %4.4s %dx%d > %4.4s %dx%d %s %s: %d frames encoded, %d system errors, %d encoding errors, %d frame errors\n",
240 		ctx->name,
241 		(char *)&frame->pixelformat,
242 		frame->aligned_width, frame->aligned_height,
243 		(char *)&stream->streamformat,
244 		stream->width, stream->height,
245 		stream->profile, stream->level,
246 		ctx->encoded_frames,
247 		ctx->sys_errors,
248 		ctx->encode_errors,
249 		ctx->frame_errors);
250 }
251 
252 /*
253  * V4L2 ioctl operations
254  */
255 
hva_querycap(struct file * file,void * priv,struct v4l2_capability * cap)256 static int hva_querycap(struct file *file, void *priv,
257 			struct v4l2_capability *cap)
258 {
259 	struct hva_ctx *ctx = file_to_ctx(file);
260 	struct hva_dev *hva = ctx_to_hdev(ctx);
261 
262 	strscpy(cap->driver, HVA_NAME, sizeof(cap->driver));
263 	strscpy(cap->card, hva->vdev->name, sizeof(cap->card));
264 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
265 		 hva->pdev->name);
266 
267 	return 0;
268 }
269 
hva_enum_fmt_stream(struct file * file,void * priv,struct v4l2_fmtdesc * f)270 static int hva_enum_fmt_stream(struct file *file, void *priv,
271 			       struct v4l2_fmtdesc *f)
272 {
273 	struct hva_ctx *ctx = file_to_ctx(file);
274 	struct hva_dev *hva = ctx_to_hdev(ctx);
275 
276 	if (unlikely(f->index >= hva->nb_of_streamformats))
277 		return -EINVAL;
278 
279 	f->pixelformat = hva->streamformats[f->index];
280 
281 	return 0;
282 }
283 
hva_enum_fmt_frame(struct file * file,void * priv,struct v4l2_fmtdesc * f)284 static int hva_enum_fmt_frame(struct file *file, void *priv,
285 			      struct v4l2_fmtdesc *f)
286 {
287 	struct hva_ctx *ctx = file_to_ctx(file);
288 	struct hva_dev *hva = ctx_to_hdev(ctx);
289 
290 	if (unlikely(f->index >= hva->nb_of_pixelformats))
291 		return -EINVAL;
292 
293 	f->pixelformat = hva->pixelformats[f->index];
294 
295 	return 0;
296 }
297 
hva_g_fmt_stream(struct file * file,void * fh,struct v4l2_format * f)298 static int hva_g_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
299 {
300 	struct hva_ctx *ctx = file_to_ctx(file);
301 	struct hva_streaminfo *streaminfo = &ctx->streaminfo;
302 
303 	f->fmt.pix.width = streaminfo->width;
304 	f->fmt.pix.height = streaminfo->height;
305 	f->fmt.pix.field = V4L2_FIELD_NONE;
306 	f->fmt.pix.colorspace = ctx->colorspace;
307 	f->fmt.pix.xfer_func = ctx->xfer_func;
308 	f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
309 	f->fmt.pix.quantization = ctx->quantization;
310 	f->fmt.pix.pixelformat = streaminfo->streamformat;
311 	f->fmt.pix.bytesperline = 0;
312 	f->fmt.pix.sizeimage = ctx->max_stream_size;
313 
314 	return 0;
315 }
316 
hva_g_fmt_frame(struct file * file,void * fh,struct v4l2_format * f)317 static int hva_g_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
318 {
319 	struct hva_ctx *ctx = file_to_ctx(file);
320 	struct hva_frameinfo *frameinfo = &ctx->frameinfo;
321 
322 	f->fmt.pix.width = frameinfo->width;
323 	f->fmt.pix.height = frameinfo->height;
324 	f->fmt.pix.field = V4L2_FIELD_NONE;
325 	f->fmt.pix.colorspace = ctx->colorspace;
326 	f->fmt.pix.xfer_func = ctx->xfer_func;
327 	f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
328 	f->fmt.pix.quantization = ctx->quantization;
329 	f->fmt.pix.pixelformat = frameinfo->pixelformat;
330 	f->fmt.pix.bytesperline = frame_stride(frameinfo->aligned_width,
331 					       frameinfo->pixelformat);
332 	f->fmt.pix.sizeimage = frameinfo->size;
333 
334 	return 0;
335 }
336 
hva_try_fmt_stream(struct file * file,void * priv,struct v4l2_format * f)337 static int hva_try_fmt_stream(struct file *file, void *priv,
338 			      struct v4l2_format *f)
339 {
340 	struct hva_ctx *ctx = file_to_ctx(file);
341 	struct device *dev = ctx_to_dev(ctx);
342 	struct v4l2_pix_format *pix = &f->fmt.pix;
343 	u32 streamformat = pix->pixelformat;
344 	const struct hva_enc *enc;
345 	u32 width, height;
346 	u32 stream_size;
347 
348 	enc = hva_find_encoder(ctx, ctx->frameinfo.pixelformat, streamformat);
349 	if (!enc) {
350 		dev_dbg(dev,
351 			"%s V4L2 TRY_FMT (CAPTURE): unsupported format %.4s\n",
352 			ctx->name, (char *)&pix->pixelformat);
353 		return -EINVAL;
354 	}
355 
356 	width = pix->width;
357 	height = pix->height;
358 	if (ctx->flags & HVA_FLAG_FRAMEINFO) {
359 		/*
360 		 * if the frame resolution is already fixed, only allow the
361 		 * same stream resolution
362 		 */
363 		pix->width = ctx->frameinfo.width;
364 		pix->height = ctx->frameinfo.height;
365 		if ((pix->width != width) || (pix->height != height))
366 			dev_dbg(dev,
367 				"%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit frame resolution\n",
368 				ctx->name, width, height,
369 				pix->width, pix->height);
370 	} else {
371 		/* adjust width & height */
372 		v4l_bound_align_image(&pix->width,
373 				      HVA_MIN_WIDTH, enc->max_width,
374 				      0,
375 				      &pix->height,
376 				      HVA_MIN_HEIGHT, enc->max_height,
377 				      0,
378 				      0);
379 
380 		if ((pix->width != width) || (pix->height != height))
381 			dev_dbg(dev,
382 				"%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
383 				ctx->name, width, height,
384 				pix->width, pix->height);
385 	}
386 
387 	stream_size = estimated_stream_size(pix->width, pix->height);
388 	if (pix->sizeimage < stream_size)
389 		pix->sizeimage = stream_size;
390 
391 	pix->bytesperline = 0;
392 	pix->colorspace = ctx->colorspace;
393 	pix->xfer_func = ctx->xfer_func;
394 	pix->ycbcr_enc = ctx->ycbcr_enc;
395 	pix->quantization = ctx->quantization;
396 	pix->field = V4L2_FIELD_NONE;
397 
398 	return 0;
399 }
400 
hva_try_fmt_frame(struct file * file,void * priv,struct v4l2_format * f)401 static int hva_try_fmt_frame(struct file *file, void *priv,
402 			     struct v4l2_format *f)
403 {
404 	struct hva_ctx *ctx = file_to_ctx(file);
405 	struct device *dev = ctx_to_dev(ctx);
406 	struct v4l2_pix_format *pix = &f->fmt.pix;
407 	u32 pixelformat = pix->pixelformat;
408 	const struct hva_enc *enc;
409 	u32 width, height;
410 
411 	enc = hva_find_encoder(ctx, pixelformat, ctx->streaminfo.streamformat);
412 	if (!enc) {
413 		dev_dbg(dev,
414 			"%s V4L2 TRY_FMT (OUTPUT): unsupported format %.4s\n",
415 			ctx->name, (char *)&pixelformat);
416 		return -EINVAL;
417 	}
418 
419 	/* adjust width & height */
420 	width = pix->width;
421 	height = pix->height;
422 	v4l_bound_align_image(&pix->width,
423 			      HVA_MIN_WIDTH, HVA_MAX_WIDTH,
424 			      frame_alignment(pixelformat) - 1,
425 			      &pix->height,
426 			      HVA_MIN_HEIGHT, HVA_MAX_HEIGHT,
427 			      frame_alignment(pixelformat) - 1,
428 			      0);
429 
430 	if ((pix->width != width) || (pix->height != height))
431 		dev_dbg(dev,
432 			"%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
433 			ctx->name, width, height, pix->width, pix->height);
434 
435 	width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
436 	height = ALIGN(pix->height, HVA_HEIGHT_ALIGNMENT);
437 
438 	if (!pix->colorspace) {
439 		pix->colorspace = V4L2_COLORSPACE_REC709;
440 		pix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
441 		pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
442 		pix->quantization = V4L2_QUANTIZATION_DEFAULT;
443 	}
444 
445 	pix->bytesperline = frame_stride(width, pixelformat);
446 	pix->sizeimage = frame_size(width, height, pixelformat);
447 	pix->field = V4L2_FIELD_NONE;
448 
449 	return 0;
450 }
451 
hva_s_fmt_stream(struct file * file,void * fh,struct v4l2_format * f)452 static int hva_s_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
453 {
454 	struct hva_ctx *ctx = file_to_ctx(file);
455 	struct device *dev = ctx_to_dev(ctx);
456 	struct vb2_queue *vq;
457 	int ret;
458 
459 	ret = hva_try_fmt_stream(file, fh, f);
460 	if (ret) {
461 		dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): unsupported format %.4s\n",
462 			ctx->name, (char *)&f->fmt.pix.pixelformat);
463 		return ret;
464 	}
465 
466 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
467 	if (vb2_is_streaming(vq)) {
468 		dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
469 			ctx->name);
470 		return -EBUSY;
471 	}
472 
473 	ctx->max_stream_size = f->fmt.pix.sizeimage;
474 	ctx->streaminfo.width = f->fmt.pix.width;
475 	ctx->streaminfo.height = f->fmt.pix.height;
476 	ctx->streaminfo.streamformat = f->fmt.pix.pixelformat;
477 	ctx->flags |= HVA_FLAG_STREAMINFO;
478 
479 	return 0;
480 }
481 
hva_s_fmt_frame(struct file * file,void * fh,struct v4l2_format * f)482 static int hva_s_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
483 {
484 	struct hva_ctx *ctx = file_to_ctx(file);
485 	struct device *dev = ctx_to_dev(ctx);
486 	struct v4l2_pix_format *pix = &f->fmt.pix;
487 	struct vb2_queue *vq;
488 	int ret;
489 
490 	ret = hva_try_fmt_frame(file, fh, f);
491 	if (ret) {
492 		dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): unsupported format %.4s\n",
493 			ctx->name, (char *)&pix->pixelformat);
494 		return ret;
495 	}
496 
497 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
498 	if (vb2_is_streaming(vq)) {
499 		dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): queue busy\n", ctx->name);
500 		return -EBUSY;
501 	}
502 
503 	ctx->colorspace = pix->colorspace;
504 	ctx->xfer_func = pix->xfer_func;
505 	ctx->ycbcr_enc = pix->ycbcr_enc;
506 	ctx->quantization = pix->quantization;
507 
508 	ctx->frameinfo.aligned_width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
509 	ctx->frameinfo.aligned_height = ALIGN(pix->height,
510 					      HVA_HEIGHT_ALIGNMENT);
511 	ctx->frameinfo.size = pix->sizeimage;
512 	ctx->frameinfo.pixelformat = pix->pixelformat;
513 	ctx->frameinfo.width = pix->width;
514 	ctx->frameinfo.height = pix->height;
515 	ctx->flags |= HVA_FLAG_FRAMEINFO;
516 
517 	return 0;
518 }
519 
hva_g_parm(struct file * file,void * fh,struct v4l2_streamparm * sp)520 static int hva_g_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
521 {
522 	struct hva_ctx *ctx = file_to_ctx(file);
523 	struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
524 
525 	if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
526 		return -EINVAL;
527 
528 	sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
529 	sp->parm.output.timeperframe.numerator = time_per_frame->numerator;
530 	sp->parm.output.timeperframe.denominator =
531 		time_per_frame->denominator;
532 
533 	return 0;
534 }
535 
hva_s_parm(struct file * file,void * fh,struct v4l2_streamparm * sp)536 static int hva_s_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
537 {
538 	struct hva_ctx *ctx = file_to_ctx(file);
539 	struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
540 
541 	if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
542 		return -EINVAL;
543 
544 	if (!sp->parm.output.timeperframe.numerator ||
545 	    !sp->parm.output.timeperframe.denominator)
546 		return hva_g_parm(file, fh, sp);
547 
548 	sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
549 	time_per_frame->numerator = sp->parm.output.timeperframe.numerator;
550 	time_per_frame->denominator =
551 		sp->parm.output.timeperframe.denominator;
552 
553 	return 0;
554 }
555 
hva_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)556 static int hva_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
557 {
558 	struct hva_ctx *ctx = file_to_ctx(file);
559 	struct device *dev = ctx_to_dev(ctx);
560 
561 	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
562 		/*
563 		 * depending on the targeted compressed video format, the
564 		 * capture buffer might contain headers (e.g. H.264 SPS/PPS)
565 		 * filled in by the driver client; the size of these data is
566 		 * copied from the bytesused field of the V4L2 buffer in the
567 		 * payload field of the hva stream buffer
568 		 */
569 		struct vb2_queue *vq;
570 		struct hva_stream *stream;
571 		struct vb2_buffer *vb2_buf;
572 
573 		vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, buf->type);
574 		vb2_buf = vb2_get_buffer(vq, buf->index);
575 		if (!vb2_buf) {
576 			dev_dbg(dev, "%s buffer index %d not found\n", ctx->name, buf->index);
577 			return -EINVAL;
578 		}
579 		stream = to_hva_stream(to_vb2_v4l2_buffer(vb2_buf));
580 		stream->bytesused = buf->bytesused;
581 	}
582 
583 	return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
584 }
585 
586 /* V4L2 ioctl ops */
587 static const struct v4l2_ioctl_ops hva_ioctl_ops = {
588 	.vidioc_querycap		= hva_querycap,
589 	.vidioc_enum_fmt_vid_cap	= hva_enum_fmt_stream,
590 	.vidioc_enum_fmt_vid_out	= hva_enum_fmt_frame,
591 	.vidioc_g_fmt_vid_cap		= hva_g_fmt_stream,
592 	.vidioc_g_fmt_vid_out		= hva_g_fmt_frame,
593 	.vidioc_try_fmt_vid_cap		= hva_try_fmt_stream,
594 	.vidioc_try_fmt_vid_out		= hva_try_fmt_frame,
595 	.vidioc_s_fmt_vid_cap		= hva_s_fmt_stream,
596 	.vidioc_s_fmt_vid_out		= hva_s_fmt_frame,
597 	.vidioc_g_parm			= hva_g_parm,
598 	.vidioc_s_parm			= hva_s_parm,
599 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
600 	.vidioc_create_bufs             = v4l2_m2m_ioctl_create_bufs,
601 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
602 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
603 	.vidioc_qbuf			= hva_qbuf,
604 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
605 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
606 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
607 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
608 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
609 };
610 
611 /*
612  * V4L2 control operations
613  */
614 
hva_s_ctrl(struct v4l2_ctrl * ctrl)615 static int hva_s_ctrl(struct v4l2_ctrl *ctrl)
616 {
617 	struct hva_ctx *ctx = container_of(ctrl->handler, struct hva_ctx,
618 					   ctrl_handler);
619 	struct device *dev = ctx_to_dev(ctx);
620 
621 	dev_dbg(dev, "%s S_CTRL: id = %d, val = %d\n", ctx->name,
622 		ctrl->id, ctrl->val);
623 
624 	switch (ctrl->id) {
625 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
626 		ctx->ctrls.bitrate_mode = ctrl->val;
627 		break;
628 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
629 		ctx->ctrls.gop_size = ctrl->val;
630 		break;
631 	case V4L2_CID_MPEG_VIDEO_BITRATE:
632 		ctx->ctrls.bitrate = ctrl->val;
633 		break;
634 	case V4L2_CID_MPEG_VIDEO_ASPECT:
635 		ctx->ctrls.aspect = ctrl->val;
636 		break;
637 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
638 		ctx->ctrls.profile = ctrl->val;
639 		snprintf(ctx->streaminfo.profile,
640 			 sizeof(ctx->streaminfo.profile),
641 			 "%s profile",
642 			 v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
643 		break;
644 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
645 		ctx->ctrls.level = ctrl->val;
646 		snprintf(ctx->streaminfo.level,
647 			 sizeof(ctx->streaminfo.level),
648 			 "level %s",
649 			 v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
650 		break;
651 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
652 		ctx->ctrls.entropy_mode = ctrl->val;
653 		break;
654 	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
655 		ctx->ctrls.cpb_size = ctrl->val;
656 		break;
657 	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
658 		ctx->ctrls.dct8x8 = ctrl->val;
659 		break;
660 	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
661 		ctx->ctrls.qpmin = ctrl->val;
662 		break;
663 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
664 		ctx->ctrls.qpmax = ctrl->val;
665 		break;
666 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
667 		ctx->ctrls.vui_sar = ctrl->val;
668 		break;
669 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
670 		ctx->ctrls.vui_sar_idc = ctrl->val;
671 		break;
672 	case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:
673 		ctx->ctrls.sei_fp = ctrl->val;
674 		break;
675 	case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
676 		ctx->ctrls.sei_fp_type = ctrl->val;
677 		break;
678 	default:
679 		dev_dbg(dev, "%s S_CTRL: invalid control (id = %d)\n",
680 			ctx->name, ctrl->id);
681 		return -EINVAL;
682 	}
683 
684 	return 0;
685 }
686 
687 /* V4L2 control ops */
688 static const struct v4l2_ctrl_ops hva_ctrl_ops = {
689 	.s_ctrl = hva_s_ctrl,
690 };
691 
hva_ctrls_setup(struct hva_ctx * ctx)692 static int hva_ctrls_setup(struct hva_ctx *ctx)
693 {
694 	struct device *dev = ctx_to_dev(ctx);
695 	u64 mask;
696 	enum v4l2_mpeg_video_h264_sei_fp_arrangement_type sei_fp_type =
697 		V4L2_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE_TOP_BOTTOM;
698 
699 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, 15);
700 
701 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
702 			       V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
703 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
704 			       0,
705 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
706 
707 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
708 			  V4L2_CID_MPEG_VIDEO_GOP_SIZE,
709 			  1, 60, 1, 16);
710 
711 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
712 			  V4L2_CID_MPEG_VIDEO_BITRATE,
713 			  1000, 60000000, 1000, 20000000);
714 
715 	mask = ~(1 << V4L2_MPEG_VIDEO_ASPECT_1x1);
716 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
717 			       V4L2_CID_MPEG_VIDEO_ASPECT,
718 			       V4L2_MPEG_VIDEO_ASPECT_1x1,
719 			       mask,
720 			       V4L2_MPEG_VIDEO_ASPECT_1x1);
721 
722 	mask = ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
723 		 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
724 		 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
725 		 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH));
726 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
727 			       V4L2_CID_MPEG_VIDEO_H264_PROFILE,
728 			       V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH,
729 			       mask,
730 			       V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
731 
732 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
733 			       V4L2_CID_MPEG_VIDEO_H264_LEVEL,
734 			       V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
735 			       0,
736 			       V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
737 
738 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
739 			       V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
740 			       V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
741 			       0,
742 			       V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
743 
744 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
745 			  V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
746 			  1, 10000, 1, 3000);
747 
748 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
749 			  V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
750 			  0, 1, 1, 0);
751 
752 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
753 			  V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
754 			  0, 51, 1, 5);
755 
756 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
757 			  V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
758 			  0, 51, 1, 51);
759 
760 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
761 			  V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
762 			  0, 1, 1, 1);
763 
764 	mask = ~(1 << V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
765 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
766 			       V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
767 			       V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1,
768 			       mask,
769 			       V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
770 
771 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
772 			  V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING,
773 			  0, 1, 1, 0);
774 
775 	mask = ~(1 << sei_fp_type);
776 	v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
777 			       V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE,
778 			       sei_fp_type,
779 			       mask,
780 			       sei_fp_type);
781 
782 	if (ctx->ctrl_handler.error) {
783 		int err = ctx->ctrl_handler.error;
784 
785 		dev_dbg(dev, "%s controls setup failed (%d)\n",
786 			ctx->name, err);
787 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
788 		return err;
789 	}
790 
791 	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
792 
793 	/* set default time per frame */
794 	ctx->ctrls.time_per_frame.numerator = HVA_DEFAULT_FRAME_NUM;
795 	ctx->ctrls.time_per_frame.denominator = HVA_DEFAULT_FRAME_DEN;
796 
797 	return 0;
798 }
799 
800 /*
801  * mem-to-mem operations
802  */
803 
hva_run_work(struct work_struct * work)804 static void hva_run_work(struct work_struct *work)
805 {
806 	struct hva_ctx *ctx = container_of(work, struct hva_ctx, run_work);
807 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
808 	const struct hva_enc *enc = ctx->enc;
809 	struct hva_frame *frame;
810 	struct hva_stream *stream;
811 	int ret;
812 
813 	/* protect instance against reentrancy */
814 	mutex_lock(&ctx->lock);
815 
816 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
817 	hva_dbg_perf_begin(ctx);
818 #endif
819 
820 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
821 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
822 
823 	frame = to_hva_frame(src_buf);
824 	stream = to_hva_stream(dst_buf);
825 	frame->vbuf.sequence = ctx->frame_num++;
826 
827 	ret = enc->encode(ctx, frame, stream);
828 
829 	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, stream->bytesused);
830 	if (ret) {
831 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
832 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
833 	} else {
834 		/* propagate frame timestamp */
835 		dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
836 		dst_buf->field = V4L2_FIELD_NONE;
837 		dst_buf->sequence = ctx->stream_num - 1;
838 
839 		ctx->encoded_frames++;
840 
841 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
842 		hva_dbg_perf_end(ctx, stream);
843 #endif
844 
845 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
846 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
847 	}
848 
849 	mutex_unlock(&ctx->lock);
850 
851 	v4l2_m2m_job_finish(ctx->hva_dev->m2m_dev, ctx->fh.m2m_ctx);
852 }
853 
hva_device_run(void * priv)854 static void hva_device_run(void *priv)
855 {
856 	struct hva_ctx *ctx = priv;
857 	struct hva_dev *hva = ctx_to_hdev(ctx);
858 
859 	queue_work(hva->work_queue, &ctx->run_work);
860 }
861 
hva_job_abort(void * priv)862 static void hva_job_abort(void *priv)
863 {
864 	struct hva_ctx *ctx = priv;
865 	struct device *dev = ctx_to_dev(ctx);
866 
867 	dev_dbg(dev, "%s aborting job\n", ctx->name);
868 
869 	ctx->aborting = true;
870 }
871 
hva_job_ready(void * priv)872 static int hva_job_ready(void *priv)
873 {
874 	struct hva_ctx *ctx = priv;
875 	struct device *dev = ctx_to_dev(ctx);
876 
877 	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
878 		dev_dbg(dev, "%s job not ready: no frame buffers\n",
879 			ctx->name);
880 		return 0;
881 	}
882 
883 	if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
884 		dev_dbg(dev, "%s job not ready: no stream buffers\n",
885 			ctx->name);
886 		return 0;
887 	}
888 
889 	if (ctx->aborting) {
890 		dev_dbg(dev, "%s job not ready: aborting\n", ctx->name);
891 		return 0;
892 	}
893 
894 	return 1;
895 }
896 
897 /* mem-to-mem ops */
898 static const struct v4l2_m2m_ops hva_m2m_ops = {
899 	.device_run	= hva_device_run,
900 	.job_abort	= hva_job_abort,
901 	.job_ready	= hva_job_ready,
902 };
903 
904 /*
905  * VB2 queue operations
906  */
907 
hva_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])908 static int hva_queue_setup(struct vb2_queue *vq,
909 			   unsigned int *num_buffers, unsigned int *num_planes,
910 			   unsigned int sizes[], struct device *alloc_devs[])
911 {
912 	struct hva_ctx *ctx = vb2_get_drv_priv(vq);
913 	struct device *dev = ctx_to_dev(ctx);
914 	unsigned int size;
915 
916 	dev_dbg(dev, "%s %s queue setup: num_buffers %d\n", ctx->name,
917 		to_type_str(vq->type), *num_buffers);
918 
919 	size = vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ?
920 		ctx->frameinfo.size : ctx->max_stream_size;
921 
922 	if (*num_planes)
923 		return sizes[0] < size ? -EINVAL : 0;
924 
925 	/* only one plane supported */
926 	*num_planes = 1;
927 	sizes[0] = size;
928 
929 	return 0;
930 }
931 
hva_buf_prepare(struct vb2_buffer * vb)932 static int hva_buf_prepare(struct vb2_buffer *vb)
933 {
934 	struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
935 	struct device *dev = ctx_to_dev(ctx);
936 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
937 
938 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
939 		struct hva_frame *frame = to_hva_frame(vbuf);
940 
941 		if (vbuf->field == V4L2_FIELD_ANY)
942 			vbuf->field = V4L2_FIELD_NONE;
943 		if (vbuf->field != V4L2_FIELD_NONE) {
944 			dev_dbg(dev,
945 				"%s frame[%d] prepare: %d field not supported\n",
946 				ctx->name, vb->index, vbuf->field);
947 			return -EINVAL;
948 		}
949 
950 		if (!frame->prepared) {
951 			/* get memory addresses */
952 			frame->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
953 			frame->paddr = vb2_dma_contig_plane_dma_addr(
954 					&vbuf->vb2_buf, 0);
955 			frame->info = ctx->frameinfo;
956 			frame->prepared = true;
957 
958 			dev_dbg(dev,
959 				"%s frame[%d] prepared; virt=%p, phy=%pad\n",
960 				ctx->name, vb->index,
961 				frame->vaddr, &frame->paddr);
962 		}
963 	} else {
964 		struct hva_stream *stream = to_hva_stream(vbuf);
965 
966 		if (!stream->prepared) {
967 			/* get memory addresses */
968 			stream->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
969 			stream->paddr = vb2_dma_contig_plane_dma_addr(
970 					&vbuf->vb2_buf, 0);
971 			stream->size = vb2_plane_size(&vbuf->vb2_buf, 0);
972 			stream->prepared = true;
973 
974 			dev_dbg(dev,
975 				"%s stream[%d] prepared; virt=%p, phy=%pad\n",
976 				ctx->name, vb->index,
977 				stream->vaddr, &stream->paddr);
978 		}
979 	}
980 
981 	return 0;
982 }
983 
hva_buf_queue(struct vb2_buffer * vb)984 static void hva_buf_queue(struct vb2_buffer *vb)
985 {
986 	struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
987 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
988 
989 	if (ctx->fh.m2m_ctx)
990 		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
991 }
992 
hva_start_streaming(struct vb2_queue * vq,unsigned int count)993 static int hva_start_streaming(struct vb2_queue *vq, unsigned int count)
994 {
995 	struct hva_ctx *ctx = vb2_get_drv_priv(vq);
996 	struct hva_dev *hva = ctx_to_hdev(ctx);
997 	struct device *dev = ctx_to_dev(ctx);
998 	struct vb2_v4l2_buffer *vbuf;
999 	int ret;
1000 	unsigned int i;
1001 	bool found = false;
1002 
1003 	dev_dbg(dev, "%s %s start streaming\n", ctx->name,
1004 		to_type_str(vq->type));
1005 
1006 	/* open encoder when both start_streaming have been called */
1007 	if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
1008 		if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->cap_q_ctx.q))
1009 			return 0;
1010 	} else {
1011 		if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->out_q_ctx.q))
1012 			return 0;
1013 	}
1014 
1015 	/* store the instance context in the instances array */
1016 	for (i = 0; i < HVA_MAX_INSTANCES; i++) {
1017 		if (!hva->instances[i]) {
1018 			hva->instances[i] = ctx;
1019 			/* save the context identifier in the context */
1020 			ctx->id = i;
1021 			found = true;
1022 			break;
1023 		}
1024 	}
1025 
1026 	if (!found) {
1027 		dev_err(dev, "%s maximum instances reached\n", ctx->name);
1028 		ret = -ENOMEM;
1029 		goto err;
1030 	}
1031 
1032 	hva->nb_of_instances++;
1033 
1034 	if (!ctx->enc) {
1035 		ret = hva_open_encoder(ctx,
1036 				       ctx->streaminfo.streamformat,
1037 				       ctx->frameinfo.pixelformat,
1038 				       &ctx->enc);
1039 		if (ret < 0)
1040 			goto err_ctx;
1041 	}
1042 
1043 	return 0;
1044 
1045 err_ctx:
1046 	hva->instances[ctx->id] = NULL;
1047 	hva->nb_of_instances--;
1048 err:
1049 	if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1050 		/* return of all pending buffers to vb2 (in queued state) */
1051 		while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1052 			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1053 	} else {
1054 		/* return of all pending buffers to vb2 (in queued state) */
1055 		while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1056 			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1057 	}
1058 
1059 	ctx->sys_errors++;
1060 
1061 	return ret;
1062 }
1063 
hva_stop_streaming(struct vb2_queue * vq)1064 static void hva_stop_streaming(struct vb2_queue *vq)
1065 {
1066 	struct hva_ctx *ctx = vb2_get_drv_priv(vq);
1067 	struct hva_dev *hva = ctx_to_hdev(ctx);
1068 	struct device *dev = ctx_to_dev(ctx);
1069 	const struct hva_enc *enc = ctx->enc;
1070 	struct vb2_v4l2_buffer *vbuf;
1071 
1072 	dev_dbg(dev, "%s %s stop streaming\n", ctx->name,
1073 		to_type_str(vq->type));
1074 
1075 	if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1076 		/* return of all pending buffers to vb2 (in error state) */
1077 		ctx->frame_num = 0;
1078 		while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1079 			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1080 	} else {
1081 		/* return of all pending buffers to vb2 (in error state) */
1082 		ctx->stream_num = 0;
1083 		while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1084 			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1085 	}
1086 
1087 	if ((V4L2_TYPE_IS_OUTPUT(vq->type) &&
1088 	     vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q)) ||
1089 	    (V4L2_TYPE_IS_CAPTURE(vq->type) &&
1090 	     vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q))) {
1091 		dev_dbg(dev, "%s %s out=%d cap=%d\n",
1092 			ctx->name, to_type_str(vq->type),
1093 			vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q),
1094 			vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q));
1095 		return;
1096 	}
1097 
1098 	/* close encoder when both stop_streaming have been called */
1099 	if (enc) {
1100 		dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
1101 		enc->close(ctx);
1102 		ctx->enc = NULL;
1103 
1104 		/* clear instance context in instances array */
1105 		hva->instances[ctx->id] = NULL;
1106 		hva->nb_of_instances--;
1107 	}
1108 
1109 	ctx->aborting = false;
1110 }
1111 
1112 /* VB2 queue ops */
1113 static const struct vb2_ops hva_qops = {
1114 	.queue_setup		= hva_queue_setup,
1115 	.buf_prepare		= hva_buf_prepare,
1116 	.buf_queue		= hva_buf_queue,
1117 	.start_streaming	= hva_start_streaming,
1118 	.stop_streaming		= hva_stop_streaming,
1119 };
1120 
1121 /*
1122  * V4L2 file operations
1123  */
1124 
queue_init(struct hva_ctx * ctx,struct vb2_queue * vq)1125 static int queue_init(struct hva_ctx *ctx, struct vb2_queue *vq)
1126 {
1127 	vq->io_modes = VB2_MMAP | VB2_DMABUF;
1128 	vq->drv_priv = ctx;
1129 	vq->ops = &hva_qops;
1130 	vq->mem_ops = &vb2_dma_contig_memops;
1131 	vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1132 	vq->lock = &ctx->hva_dev->lock;
1133 
1134 	return vb2_queue_init(vq);
1135 }
1136 
hva_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1137 static int hva_queue_init(void *priv, struct vb2_queue *src_vq,
1138 			  struct vb2_queue *dst_vq)
1139 {
1140 	struct hva_ctx *ctx = priv;
1141 	int ret;
1142 
1143 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1144 	src_vq->buf_struct_size = sizeof(struct hva_frame);
1145 	src_vq->min_queued_buffers = MIN_FRAMES;
1146 	src_vq->dev = ctx->hva_dev->dev;
1147 
1148 	ret = queue_init(ctx, src_vq);
1149 	if (ret)
1150 		return ret;
1151 
1152 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1153 	dst_vq->buf_struct_size = sizeof(struct hva_stream);
1154 	dst_vq->min_queued_buffers = MIN_STREAMS;
1155 	dst_vq->dev = ctx->hva_dev->dev;
1156 
1157 	return queue_init(ctx, dst_vq);
1158 }
1159 
hva_open(struct file * file)1160 static int hva_open(struct file *file)
1161 {
1162 	struct hva_dev *hva = video_drvdata(file);
1163 	struct device *dev = hva_to_dev(hva);
1164 	struct hva_ctx *ctx;
1165 	int ret;
1166 
1167 	ctx = kzalloc_obj(*ctx);
1168 	if (!ctx) {
1169 		ret = -ENOMEM;
1170 		goto out;
1171 	}
1172 	ctx->hva_dev = hva;
1173 
1174 	INIT_WORK(&ctx->run_work, hva_run_work);
1175 	v4l2_fh_init(&ctx->fh, video_devdata(file));
1176 	v4l2_fh_add(&ctx->fh, file);
1177 
1178 	ret = hva_ctrls_setup(ctx);
1179 	if (ret) {
1180 		dev_err(dev, "%s [x:x] failed to setup controls\n",
1181 			HVA_PREFIX);
1182 		ctx->sys_errors++;
1183 		goto err_fh;
1184 	}
1185 	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
1186 
1187 	mutex_init(&ctx->lock);
1188 
1189 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(hva->m2m_dev, ctx,
1190 					    &hva_queue_init);
1191 	if (IS_ERR(ctx->fh.m2m_ctx)) {
1192 		ret = PTR_ERR(ctx->fh.m2m_ctx);
1193 		dev_err(dev, "%s failed to initialize m2m context (%d)\n",
1194 			HVA_PREFIX, ret);
1195 		ctx->sys_errors++;
1196 		goto err_ctrls;
1197 	}
1198 
1199 	/* set the instance name */
1200 	mutex_lock(&hva->lock);
1201 	hva->instance_id++;
1202 	snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]",
1203 		 hva->instance_id);
1204 	mutex_unlock(&hva->lock);
1205 
1206 	/* default parameters for frame and stream */
1207 	set_default_params(ctx);
1208 
1209 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1210 	hva_dbg_ctx_create(ctx);
1211 #endif
1212 
1213 	dev_info(dev, "%s encoder instance created\n", ctx->name);
1214 
1215 	return 0;
1216 
1217 err_ctrls:
1218 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1219 err_fh:
1220 	v4l2_fh_del(&ctx->fh, file);
1221 	v4l2_fh_exit(&ctx->fh);
1222 	kfree(ctx);
1223 out:
1224 	return ret;
1225 }
1226 
hva_release(struct file * file)1227 static int hva_release(struct file *file)
1228 {
1229 	struct hva_ctx *ctx = file_to_ctx(file);
1230 	struct hva_dev *hva = ctx_to_hdev(ctx);
1231 	struct device *dev = ctx_to_dev(ctx);
1232 	const struct hva_enc *enc = ctx->enc;
1233 
1234 	if (enc) {
1235 		dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
1236 		enc->close(ctx);
1237 		ctx->enc = NULL;
1238 
1239 		/* clear instance context in instances array */
1240 		hva->instances[ctx->id] = NULL;
1241 		hva->nb_of_instances--;
1242 	}
1243 
1244 	/* trace a summary of instance before closing (debug purpose) */
1245 	hva_dbg_summary(ctx);
1246 
1247 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1248 
1249 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1250 
1251 	v4l2_fh_del(&ctx->fh, file);
1252 	v4l2_fh_exit(&ctx->fh);
1253 
1254 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1255 	hva_dbg_ctx_remove(ctx);
1256 #endif
1257 
1258 	dev_info(dev, "%s encoder instance released\n", ctx->name);
1259 
1260 	kfree(ctx);
1261 
1262 	return 0;
1263 }
1264 
1265 /* V4L2 file ops */
1266 static const struct v4l2_file_operations hva_fops = {
1267 	.owner			= THIS_MODULE,
1268 	.open			= hva_open,
1269 	.release		= hva_release,
1270 	.unlocked_ioctl		= video_ioctl2,
1271 	.mmap			= v4l2_m2m_fop_mmap,
1272 	.poll			= v4l2_m2m_fop_poll,
1273 };
1274 
1275 /*
1276  * Platform device operations
1277  */
1278 
hva_register_device(struct hva_dev * hva)1279 static int hva_register_device(struct hva_dev *hva)
1280 {
1281 	int ret;
1282 	struct video_device *vdev;
1283 	struct device *dev;
1284 
1285 	if (!hva)
1286 		return -ENODEV;
1287 	dev = hva_to_dev(hva);
1288 
1289 	hva->m2m_dev = v4l2_m2m_init(&hva_m2m_ops);
1290 	if (IS_ERR(hva->m2m_dev)) {
1291 		dev_err(dev, "%s failed to initialize v4l2-m2m device\n",
1292 			HVA_PREFIX);
1293 		ret = PTR_ERR(hva->m2m_dev);
1294 		goto err;
1295 	}
1296 
1297 	vdev = video_device_alloc();
1298 	if (!vdev) {
1299 		dev_err(dev, "%s failed to allocate video device\n",
1300 			HVA_PREFIX);
1301 		ret = -ENOMEM;
1302 		goto err_m2m_release;
1303 	}
1304 
1305 	vdev->fops = &hva_fops;
1306 	vdev->ioctl_ops = &hva_ioctl_ops;
1307 	vdev->release = video_device_release;
1308 	vdev->lock = &hva->lock;
1309 	vdev->vfl_dir = VFL_DIR_M2M;
1310 	vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
1311 	vdev->v4l2_dev = &hva->v4l2_dev;
1312 	snprintf(vdev->name, sizeof(vdev->name), "%s%lx", HVA_NAME,
1313 		 hva->ip_version);
1314 
1315 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1316 	if (ret) {
1317 		dev_err(dev, "%s failed to register video device\n",
1318 			HVA_PREFIX);
1319 		goto err_vdev_release;
1320 	}
1321 
1322 	hva->vdev = vdev;
1323 	video_set_drvdata(vdev, hva);
1324 	return 0;
1325 
1326 err_vdev_release:
1327 	video_device_release(vdev);
1328 err_m2m_release:
1329 	v4l2_m2m_release(hva->m2m_dev);
1330 err:
1331 	return ret;
1332 }
1333 
hva_unregister_device(struct hva_dev * hva)1334 static void hva_unregister_device(struct hva_dev *hva)
1335 {
1336 	if (!hva)
1337 		return;
1338 
1339 	if (hva->m2m_dev)
1340 		v4l2_m2m_release(hva->m2m_dev);
1341 
1342 	video_unregister_device(hva->vdev);
1343 }
1344 
hva_probe(struct platform_device * pdev)1345 static int hva_probe(struct platform_device *pdev)
1346 {
1347 	struct hva_dev *hva;
1348 	struct device *dev = &pdev->dev;
1349 	int ret;
1350 
1351 	hva = devm_kzalloc(dev, sizeof(*hva), GFP_KERNEL);
1352 	if (!hva) {
1353 		ret = -ENOMEM;
1354 		goto err;
1355 	}
1356 
1357 	ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
1358 	if (ret)
1359 		return ret;
1360 
1361 	hva->dev = dev;
1362 	hva->pdev = pdev;
1363 	platform_set_drvdata(pdev, hva);
1364 
1365 	mutex_init(&hva->lock);
1366 
1367 	/* probe hardware */
1368 	ret = hva_hw_probe(pdev, hva);
1369 	if (ret)
1370 		goto err;
1371 
1372 	/* register all available encoders */
1373 	register_encoders(hva);
1374 
1375 	/* register all supported formats */
1376 	register_formats(hva);
1377 
1378 	/* register on V4L2 */
1379 	ret = v4l2_device_register(dev, &hva->v4l2_dev);
1380 	if (ret) {
1381 		dev_err(dev, "%s %s failed to register V4L2 device\n",
1382 			HVA_PREFIX, HVA_NAME);
1383 		goto err_hw;
1384 	}
1385 
1386 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1387 	hva_debugfs_create(hva);
1388 #endif
1389 
1390 	hva->work_queue = create_workqueue(HVA_NAME);
1391 	if (!hva->work_queue) {
1392 		dev_err(dev, "%s %s failed to allocate work queue\n",
1393 			HVA_PREFIX, HVA_NAME);
1394 		ret = -ENOMEM;
1395 		goto err_v4l2;
1396 	}
1397 
1398 	/* register device */
1399 	ret = hva_register_device(hva);
1400 	if (ret)
1401 		goto err_work_queue;
1402 
1403 	dev_info(dev, "%s %s registered as /dev/video%d\n", HVA_PREFIX,
1404 		 HVA_NAME, hva->vdev->num);
1405 
1406 	return 0;
1407 
1408 err_work_queue:
1409 	destroy_workqueue(hva->work_queue);
1410 err_v4l2:
1411 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1412 	hva_debugfs_remove(hva);
1413 #endif
1414 	v4l2_device_unregister(&hva->v4l2_dev);
1415 err_hw:
1416 	hva_hw_remove(hva);
1417 err:
1418 	return ret;
1419 }
1420 
hva_remove(struct platform_device * pdev)1421 static void hva_remove(struct platform_device *pdev)
1422 {
1423 	struct hva_dev *hva = platform_get_drvdata(pdev);
1424 	struct device *dev = hva_to_dev(hva);
1425 
1426 	hva_unregister_device(hva);
1427 
1428 	destroy_workqueue(hva->work_queue);
1429 
1430 	hva_hw_remove(hva);
1431 
1432 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1433 	hva_debugfs_remove(hva);
1434 #endif
1435 
1436 	v4l2_device_unregister(&hva->v4l2_dev);
1437 
1438 	dev_info(dev, "%s %s removed\n", HVA_PREFIX, pdev->name);
1439 }
1440 
1441 /* PM ops */
1442 static const struct dev_pm_ops hva_pm_ops = {
1443 	.runtime_suspend	= hva_hw_runtime_suspend,
1444 	.runtime_resume		= hva_hw_runtime_resume,
1445 };
1446 
1447 static const struct of_device_id hva_match_types[] = {
1448 	{
1449 	 .compatible = "st,st-hva",
1450 	},
1451 	{ /* end node */ }
1452 };
1453 
1454 MODULE_DEVICE_TABLE(of, hva_match_types);
1455 
1456 static struct platform_driver hva_driver = {
1457 	.probe  = hva_probe,
1458 	.remove = hva_remove,
1459 	.driver = {
1460 		.name		= HVA_NAME,
1461 		.of_match_table	= hva_match_types,
1462 		.pm		= &hva_pm_ops,
1463 		},
1464 };
1465 
1466 module_platform_driver(hva_driver);
1467 
1468 MODULE_LICENSE("GPL");
1469 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
1470 MODULE_DESCRIPTION("STMicroelectronics HVA video encoder V4L2 driver");
1471