1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Rockchip Video Decoder driver
4  *
5  * Copyright (C) 2019 Collabora, Ltd.
6  *
7  * Based on rkvdec driver by Google LLC. (Tomasz Figa <tfiga@chromium.org>)
8  * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
9  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/videodev2.h>
21 #include <linux/workqueue.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/videobuf2-core.h>
25 #include <media/videobuf2-vmalloc.h>
26 
27 #include "rkvdec.h"
28 #include "rkvdec-regs.h"
29 
30 static int rkvdec_try_ctrl(struct v4l2_ctrl *ctrl)
31 {
32 	if (ctrl->id == V4L2_CID_STATELESS_H264_SPS) {
33 		const struct v4l2_ctrl_h264_sps *sps = ctrl->p_new.p_h264_sps;
34 		/*
35 		 * TODO: The hardware supports 10-bit and 4:2:2 profiles,
36 		 * but it's currently broken in the driver.
37 		 * Reject them for now, until it's fixed.
38 		 */
39 		if (sps->chroma_format_idc > 1)
40 			/* Only 4:0:0 and 4:2:0 are supported */
41 			return -EINVAL;
42 		if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8)
43 			/* Luma and chroma bit depth mismatch */
44 			return -EINVAL;
45 		if (sps->bit_depth_luma_minus8 != 0)
46 			/* Only 8-bit is supported */
47 			return -EINVAL;
48 	}
49 	return 0;
50 }
51 
52 static const struct v4l2_ctrl_ops rkvdec_ctrl_ops = {
53 	.try_ctrl = rkvdec_try_ctrl,
54 };
55 
56 static const struct rkvdec_ctrl_desc rkvdec_h264_ctrl_descs[] = {
57 	{
58 		.cfg.id = V4L2_CID_STATELESS_H264_DECODE_PARAMS,
59 	},
60 	{
61 		.cfg.id = V4L2_CID_STATELESS_H264_SPS,
62 		.cfg.ops = &rkvdec_ctrl_ops,
63 	},
64 	{
65 		.cfg.id = V4L2_CID_STATELESS_H264_PPS,
66 	},
67 	{
68 		.cfg.id = V4L2_CID_STATELESS_H264_SCALING_MATRIX,
69 	},
70 	{
71 		.cfg.id = V4L2_CID_STATELESS_H264_DECODE_MODE,
72 		.cfg.min = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
73 		.cfg.max = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
74 		.cfg.def = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
75 	},
76 	{
77 		.cfg.id = V4L2_CID_STATELESS_H264_START_CODE,
78 		.cfg.min = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
79 		.cfg.def = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
80 		.cfg.max = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
81 	},
82 	{
83 		.cfg.id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
84 		.cfg.min = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
85 		.cfg.max = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
86 		.cfg.menu_skip_mask =
87 			BIT(V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED),
88 		.cfg.def = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
89 	},
90 	{
91 		.cfg.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
92 		.cfg.min = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
93 		.cfg.max = V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
94 	},
95 };
96 
97 static const struct rkvdec_ctrls rkvdec_h264_ctrls = {
98 	.ctrls = rkvdec_h264_ctrl_descs,
99 	.num_ctrls = ARRAY_SIZE(rkvdec_h264_ctrl_descs),
100 };
101 
102 static const u32 rkvdec_h264_decoded_fmts[] = {
103 	V4L2_PIX_FMT_NV12,
104 };
105 
106 static const struct rkvdec_coded_fmt_desc rkvdec_coded_fmts[] = {
107 	{
108 		.fourcc = V4L2_PIX_FMT_H264_SLICE,
109 		.frmsize = {
110 			.min_width = 48,
111 			.max_width = 4096,
112 			.step_width = 16,
113 			.min_height = 48,
114 			.max_height = 2304,
115 			.step_height = 16,
116 		},
117 		.ctrls = &rkvdec_h264_ctrls,
118 		.ops = &rkvdec_h264_fmt_ops,
119 		.num_decoded_fmts = ARRAY_SIZE(rkvdec_h264_decoded_fmts),
120 		.decoded_fmts = rkvdec_h264_decoded_fmts,
121 	}
122 };
123 
124 static const struct rkvdec_coded_fmt_desc *
125 rkvdec_find_coded_fmt_desc(u32 fourcc)
126 {
127 	unsigned int i;
128 
129 	for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++) {
130 		if (rkvdec_coded_fmts[i].fourcc == fourcc)
131 			return &rkvdec_coded_fmts[i];
132 	}
133 
134 	return NULL;
135 }
136 
137 static void rkvdec_reset_fmt(struct rkvdec_ctx *ctx, struct v4l2_format *f,
138 			     u32 fourcc)
139 {
140 	memset(f, 0, sizeof(*f));
141 	f->fmt.pix_mp.pixelformat = fourcc;
142 	f->fmt.pix_mp.field = V4L2_FIELD_NONE;
143 	f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
144 	f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
145 	f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
146 	f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
147 }
148 
149 static void rkvdec_reset_coded_fmt(struct rkvdec_ctx *ctx)
150 {
151 	struct v4l2_format *f = &ctx->coded_fmt;
152 
153 	ctx->coded_fmt_desc = &rkvdec_coded_fmts[0];
154 	rkvdec_reset_fmt(ctx, f, ctx->coded_fmt_desc->fourcc);
155 
156 	f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
157 	f->fmt.pix_mp.width = ctx->coded_fmt_desc->frmsize.min_width;
158 	f->fmt.pix_mp.height = ctx->coded_fmt_desc->frmsize.min_height;
159 
160 	if (ctx->coded_fmt_desc->ops->adjust_fmt)
161 		ctx->coded_fmt_desc->ops->adjust_fmt(ctx, f);
162 }
163 
164 static void rkvdec_reset_decoded_fmt(struct rkvdec_ctx *ctx)
165 {
166 	struct v4l2_format *f = &ctx->decoded_fmt;
167 
168 	rkvdec_reset_fmt(ctx, f, ctx->coded_fmt_desc->decoded_fmts[0]);
169 	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
170 	v4l2_fill_pixfmt_mp(&f->fmt.pix_mp,
171 			    ctx->coded_fmt_desc->decoded_fmts[0],
172 			    ctx->coded_fmt.fmt.pix_mp.width,
173 			    ctx->coded_fmt.fmt.pix_mp.height);
174 	f->fmt.pix_mp.plane_fmt[0].sizeimage += 128 *
175 		DIV_ROUND_UP(f->fmt.pix_mp.width, 16) *
176 		DIV_ROUND_UP(f->fmt.pix_mp.height, 16);
177 }
178 
179 static int rkvdec_enum_framesizes(struct file *file, void *priv,
180 				  struct v4l2_frmsizeenum *fsize)
181 {
182 	const struct rkvdec_coded_fmt_desc *fmt;
183 
184 	if (fsize->index != 0)
185 		return -EINVAL;
186 
187 	fmt = rkvdec_find_coded_fmt_desc(fsize->pixel_format);
188 	if (!fmt)
189 		return -EINVAL;
190 
191 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
192 	fsize->stepwise = fmt->frmsize;
193 	return 0;
194 }
195 
196 static int rkvdec_querycap(struct file *file, void *priv,
197 			   struct v4l2_capability *cap)
198 {
199 	struct rkvdec_dev *rkvdec = video_drvdata(file);
200 	struct video_device *vdev = video_devdata(file);
201 
202 	strscpy(cap->driver, rkvdec->dev->driver->name,
203 		sizeof(cap->driver));
204 	strscpy(cap->card, vdev->name, sizeof(cap->card));
205 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
206 		 rkvdec->dev->driver->name);
207 	return 0;
208 }
209 
210 static int rkvdec_try_capture_fmt(struct file *file, void *priv,
211 				  struct v4l2_format *f)
212 {
213 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
214 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
215 	const struct rkvdec_coded_fmt_desc *coded_desc;
216 	unsigned int i;
217 
218 	/*
219 	 * The codec context should point to a coded format desc, if the format
220 	 * on the coded end has not been set yet, it should point to the
221 	 * default value.
222 	 */
223 	coded_desc = ctx->coded_fmt_desc;
224 	if (WARN_ON(!coded_desc))
225 		return -EINVAL;
226 
227 	for (i = 0; i < coded_desc->num_decoded_fmts; i++) {
228 		if (coded_desc->decoded_fmts[i] == pix_mp->pixelformat)
229 			break;
230 	}
231 
232 	if (i == coded_desc->num_decoded_fmts)
233 		pix_mp->pixelformat = coded_desc->decoded_fmts[0];
234 
235 	/* Always apply the frmsize constraint of the coded end. */
236 	v4l2_apply_frmsize_constraints(&pix_mp->width,
237 				       &pix_mp->height,
238 				       &coded_desc->frmsize);
239 
240 	v4l2_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat,
241 			    pix_mp->width, pix_mp->height);
242 	pix_mp->plane_fmt[0].sizeimage +=
243 		128 *
244 		DIV_ROUND_UP(pix_mp->width, 16) *
245 		DIV_ROUND_UP(pix_mp->height, 16);
246 	pix_mp->field = V4L2_FIELD_NONE;
247 
248 	return 0;
249 }
250 
251 static int rkvdec_try_output_fmt(struct file *file, void *priv,
252 				 struct v4l2_format *f)
253 {
254 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
255 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
256 	const struct rkvdec_coded_fmt_desc *desc;
257 
258 	desc = rkvdec_find_coded_fmt_desc(pix_mp->pixelformat);
259 	if (!desc) {
260 		pix_mp->pixelformat = rkvdec_coded_fmts[0].fourcc;
261 		desc = &rkvdec_coded_fmts[0];
262 	}
263 
264 	v4l2_apply_frmsize_constraints(&pix_mp->width,
265 				       &pix_mp->height,
266 				       &desc->frmsize);
267 
268 	pix_mp->field = V4L2_FIELD_NONE;
269 	/* All coded formats are considered single planar for now. */
270 	pix_mp->num_planes = 1;
271 
272 	if (desc->ops->adjust_fmt) {
273 		int ret;
274 
275 		ret = desc->ops->adjust_fmt(ctx, f);
276 		if (ret)
277 			return ret;
278 	}
279 
280 	return 0;
281 }
282 
283 static int rkvdec_s_capture_fmt(struct file *file, void *priv,
284 				struct v4l2_format *f)
285 {
286 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
287 	struct vb2_queue *vq;
288 	int ret;
289 
290 	/* Change not allowed if queue is busy */
291 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
292 			     V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
293 	if (vb2_is_busy(vq))
294 		return -EBUSY;
295 
296 	ret = rkvdec_try_capture_fmt(file, priv, f);
297 	if (ret)
298 		return ret;
299 
300 	ctx->decoded_fmt = *f;
301 	return 0;
302 }
303 
304 static int rkvdec_s_output_fmt(struct file *file, void *priv,
305 			       struct v4l2_format *f)
306 {
307 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
308 	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
309 	const struct rkvdec_coded_fmt_desc *desc;
310 	struct v4l2_format *cap_fmt;
311 	struct vb2_queue *peer_vq, *vq;
312 	int ret;
313 
314 	/*
315 	 * In order to support dynamic resolution change, the decoder admits
316 	 * a resolution change, as long as the pixelformat remains. Can't be
317 	 * done if streaming.
318 	 */
319 	vq = v4l2_m2m_get_vq(m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
320 	if (vb2_is_streaming(vq) ||
321 	    (vb2_is_busy(vq) &&
322 	     f->fmt.pix_mp.pixelformat != ctx->coded_fmt.fmt.pix_mp.pixelformat))
323 		return -EBUSY;
324 
325 	/*
326 	 * Since format change on the OUTPUT queue will reset the CAPTURE
327 	 * queue, we can't allow doing so when the CAPTURE queue has buffers
328 	 * allocated.
329 	 */
330 	peer_vq = v4l2_m2m_get_vq(m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
331 	if (vb2_is_busy(peer_vq))
332 		return -EBUSY;
333 
334 	ret = rkvdec_try_output_fmt(file, priv, f);
335 	if (ret)
336 		return ret;
337 
338 	desc = rkvdec_find_coded_fmt_desc(f->fmt.pix_mp.pixelformat);
339 	if (!desc)
340 		return -EINVAL;
341 	ctx->coded_fmt_desc = desc;
342 	ctx->coded_fmt = *f;
343 
344 	/*
345 	 * Current decoded format might have become invalid with newly
346 	 * selected codec, so reset it to default just to be safe and
347 	 * keep internal driver state sane. User is mandated to set
348 	 * the decoded format again after we return, so we don't need
349 	 * anything smarter.
350 	 *
351 	 * Note that this will propagates any size changes to the decoded format.
352 	 */
353 	rkvdec_reset_decoded_fmt(ctx);
354 
355 	/* Propagate colorspace information to capture. */
356 	cap_fmt = &ctx->decoded_fmt;
357 	cap_fmt->fmt.pix_mp.colorspace = f->fmt.pix_mp.colorspace;
358 	cap_fmt->fmt.pix_mp.xfer_func = f->fmt.pix_mp.xfer_func;
359 	cap_fmt->fmt.pix_mp.ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
360 	cap_fmt->fmt.pix_mp.quantization = f->fmt.pix_mp.quantization;
361 
362 	return 0;
363 }
364 
365 static int rkvdec_g_output_fmt(struct file *file, void *priv,
366 			       struct v4l2_format *f)
367 {
368 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
369 
370 	*f = ctx->coded_fmt;
371 	return 0;
372 }
373 
374 static int rkvdec_g_capture_fmt(struct file *file, void *priv,
375 				struct v4l2_format *f)
376 {
377 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
378 
379 	*f = ctx->decoded_fmt;
380 	return 0;
381 }
382 
383 static int rkvdec_enum_output_fmt(struct file *file, void *priv,
384 				  struct v4l2_fmtdesc *f)
385 {
386 	if (f->index >= ARRAY_SIZE(rkvdec_coded_fmts))
387 		return -EINVAL;
388 
389 	f->pixelformat = rkvdec_coded_fmts[f->index].fourcc;
390 	return 0;
391 }
392 
393 static int rkvdec_enum_capture_fmt(struct file *file, void *priv,
394 				   struct v4l2_fmtdesc *f)
395 {
396 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
397 
398 	if (WARN_ON(!ctx->coded_fmt_desc))
399 		return -EINVAL;
400 
401 	if (f->index >= ctx->coded_fmt_desc->num_decoded_fmts)
402 		return -EINVAL;
403 
404 	f->pixelformat = ctx->coded_fmt_desc->decoded_fmts[f->index];
405 	return 0;
406 }
407 
408 static const struct v4l2_ioctl_ops rkvdec_ioctl_ops = {
409 	.vidioc_querycap = rkvdec_querycap,
410 	.vidioc_enum_framesizes = rkvdec_enum_framesizes,
411 
412 	.vidioc_try_fmt_vid_cap_mplane = rkvdec_try_capture_fmt,
413 	.vidioc_try_fmt_vid_out_mplane = rkvdec_try_output_fmt,
414 	.vidioc_s_fmt_vid_out_mplane = rkvdec_s_output_fmt,
415 	.vidioc_s_fmt_vid_cap_mplane = rkvdec_s_capture_fmt,
416 	.vidioc_g_fmt_vid_out_mplane = rkvdec_g_output_fmt,
417 	.vidioc_g_fmt_vid_cap_mplane = rkvdec_g_capture_fmt,
418 	.vidioc_enum_fmt_vid_out = rkvdec_enum_output_fmt,
419 	.vidioc_enum_fmt_vid_cap = rkvdec_enum_capture_fmt,
420 
421 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
422 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
423 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
424 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
425 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
426 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
427 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
428 
429 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
430 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
431 
432 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
433 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
434 };
435 
436 static int rkvdec_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
437 			      unsigned int *num_planes, unsigned int sizes[],
438 			      struct device *alloc_devs[])
439 {
440 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
441 	struct v4l2_format *f;
442 	unsigned int i;
443 
444 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
445 		f = &ctx->coded_fmt;
446 	else
447 		f = &ctx->decoded_fmt;
448 
449 	if (*num_planes) {
450 		if (*num_planes != f->fmt.pix_mp.num_planes)
451 			return -EINVAL;
452 
453 		for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
454 			if (sizes[i] < f->fmt.pix_mp.plane_fmt[i].sizeimage)
455 				return -EINVAL;
456 		}
457 	} else {
458 		*num_planes = f->fmt.pix_mp.num_planes;
459 		for (i = 0; i < f->fmt.pix_mp.num_planes; i++)
460 			sizes[i] = f->fmt.pix_mp.plane_fmt[i].sizeimage;
461 	}
462 
463 	return 0;
464 }
465 
466 static int rkvdec_buf_prepare(struct vb2_buffer *vb)
467 {
468 	struct vb2_queue *vq = vb->vb2_queue;
469 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
470 	struct v4l2_format *f;
471 	unsigned int i;
472 
473 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
474 		f = &ctx->coded_fmt;
475 	else
476 		f = &ctx->decoded_fmt;
477 
478 	for (i = 0; i < f->fmt.pix_mp.num_planes; ++i) {
479 		u32 sizeimage = f->fmt.pix_mp.plane_fmt[i].sizeimage;
480 
481 		if (vb2_plane_size(vb, i) < sizeimage)
482 			return -EINVAL;
483 	}
484 
485 	/*
486 	 * Buffer's bytesused must be written by driver for CAPTURE buffers.
487 	 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
488 	 * it to buffer length).
489 	 */
490 	if (V4L2_TYPE_IS_CAPTURE(vq->type))
491 		vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
492 
493 	return 0;
494 }
495 
496 static void rkvdec_buf_queue(struct vb2_buffer *vb)
497 {
498 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
499 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
500 
501 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
502 }
503 
504 static int rkvdec_buf_out_validate(struct vb2_buffer *vb)
505 {
506 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
507 
508 	vbuf->field = V4L2_FIELD_NONE;
509 	return 0;
510 }
511 
512 static void rkvdec_buf_request_complete(struct vb2_buffer *vb)
513 {
514 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
515 
516 	v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_hdl);
517 }
518 
519 static int rkvdec_start_streaming(struct vb2_queue *q, unsigned int count)
520 {
521 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(q);
522 	const struct rkvdec_coded_fmt_desc *desc;
523 	int ret;
524 
525 	if (V4L2_TYPE_IS_CAPTURE(q->type))
526 		return 0;
527 
528 	desc = ctx->coded_fmt_desc;
529 	if (WARN_ON(!desc))
530 		return -EINVAL;
531 
532 	if (desc->ops->start) {
533 		ret = desc->ops->start(ctx);
534 		if (ret)
535 			return ret;
536 	}
537 
538 	return 0;
539 }
540 
541 static void rkvdec_queue_cleanup(struct vb2_queue *vq, u32 state)
542 {
543 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
544 
545 	while (true) {
546 		struct vb2_v4l2_buffer *vbuf;
547 
548 		if (V4L2_TYPE_IS_OUTPUT(vq->type))
549 			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
550 		else
551 			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
552 
553 		if (!vbuf)
554 			break;
555 
556 		v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
557 					   &ctx->ctrl_hdl);
558 		v4l2_m2m_buf_done(vbuf, state);
559 	}
560 }
561 
562 static void rkvdec_stop_streaming(struct vb2_queue *q)
563 {
564 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(q);
565 
566 	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
567 		const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
568 
569 		if (WARN_ON(!desc))
570 			return;
571 
572 		if (desc->ops->stop)
573 			desc->ops->stop(ctx);
574 	}
575 
576 	rkvdec_queue_cleanup(q, VB2_BUF_STATE_ERROR);
577 }
578 
579 static const struct vb2_ops rkvdec_queue_ops = {
580 	.queue_setup = rkvdec_queue_setup,
581 	.buf_prepare = rkvdec_buf_prepare,
582 	.buf_queue = rkvdec_buf_queue,
583 	.buf_out_validate = rkvdec_buf_out_validate,
584 	.buf_request_complete = rkvdec_buf_request_complete,
585 	.start_streaming = rkvdec_start_streaming,
586 	.stop_streaming = rkvdec_stop_streaming,
587 	.wait_prepare = vb2_ops_wait_prepare,
588 	.wait_finish = vb2_ops_wait_finish,
589 };
590 
591 static int rkvdec_request_validate(struct media_request *req)
592 {
593 	unsigned int count;
594 
595 	count = vb2_request_buffer_cnt(req);
596 	if (!count)
597 		return -ENOENT;
598 	else if (count > 1)
599 		return -EINVAL;
600 
601 	return vb2_request_validate(req);
602 }
603 
604 static const struct media_device_ops rkvdec_media_ops = {
605 	.req_validate = rkvdec_request_validate,
606 	.req_queue = v4l2_m2m_request_queue,
607 };
608 
609 static void rkvdec_job_finish_no_pm(struct rkvdec_ctx *ctx,
610 				    enum vb2_buffer_state result)
611 {
612 	if (ctx->coded_fmt_desc->ops->done) {
613 		struct vb2_v4l2_buffer *src_buf, *dst_buf;
614 
615 		src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
616 		dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
617 		ctx->coded_fmt_desc->ops->done(ctx, src_buf, dst_buf, result);
618 	}
619 
620 	v4l2_m2m_buf_done_and_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx,
621 					 result);
622 }
623 
624 static void rkvdec_job_finish(struct rkvdec_ctx *ctx,
625 			      enum vb2_buffer_state result)
626 {
627 	struct rkvdec_dev *rkvdec = ctx->dev;
628 
629 	pm_runtime_mark_last_busy(rkvdec->dev);
630 	pm_runtime_put_autosuspend(rkvdec->dev);
631 	rkvdec_job_finish_no_pm(ctx, result);
632 }
633 
634 void rkvdec_run_preamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run)
635 {
636 	struct media_request *src_req;
637 
638 	memset(run, 0, sizeof(*run));
639 
640 	run->bufs.src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
641 	run->bufs.dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
642 
643 	/* Apply request(s) controls if needed. */
644 	src_req = run->bufs.src->vb2_buf.req_obj.req;
645 	if (src_req)
646 		v4l2_ctrl_request_setup(src_req, &ctx->ctrl_hdl);
647 
648 	v4l2_m2m_buf_copy_metadata(run->bufs.src, run->bufs.dst, true);
649 }
650 
651 void rkvdec_run_postamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run)
652 {
653 	struct media_request *src_req = run->bufs.src->vb2_buf.req_obj.req;
654 
655 	if (src_req)
656 		v4l2_ctrl_request_complete(src_req, &ctx->ctrl_hdl);
657 }
658 
659 static void rkvdec_device_run(void *priv)
660 {
661 	struct rkvdec_ctx *ctx = priv;
662 	struct rkvdec_dev *rkvdec = ctx->dev;
663 	const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
664 	int ret;
665 
666 	if (WARN_ON(!desc))
667 		return;
668 
669 	ret = pm_runtime_resume_and_get(rkvdec->dev);
670 	if (ret < 0) {
671 		rkvdec_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
672 		return;
673 	}
674 
675 	ret = desc->ops->run(ctx);
676 	if (ret)
677 		rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
678 }
679 
680 static struct v4l2_m2m_ops rkvdec_m2m_ops = {
681 	.device_run = rkvdec_device_run,
682 };
683 
684 static int rkvdec_queue_init(void *priv,
685 			     struct vb2_queue *src_vq,
686 			     struct vb2_queue *dst_vq)
687 {
688 	struct rkvdec_ctx *ctx = priv;
689 	struct rkvdec_dev *rkvdec = ctx->dev;
690 	int ret;
691 
692 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
693 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
694 	src_vq->drv_priv = ctx;
695 	src_vq->ops = &rkvdec_queue_ops;
696 	src_vq->mem_ops = &vb2_dma_contig_memops;
697 
698 	/*
699 	 * Driver does mostly sequential access, so sacrifice TLB efficiency
700 	 * for faster allocation. Also, no CPU access on the source queue,
701 	 * so no kernel mapping needed.
702 	 */
703 	src_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
704 			    DMA_ATTR_NO_KERNEL_MAPPING;
705 	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
706 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
707 	src_vq->lock = &rkvdec->vdev_lock;
708 	src_vq->dev = rkvdec->v4l2_dev.dev;
709 	src_vq->supports_requests = true;
710 	src_vq->requires_requests = true;
711 
712 	ret = vb2_queue_init(src_vq);
713 	if (ret)
714 		return ret;
715 
716 	dst_vq->bidirectional = true;
717 	dst_vq->mem_ops = &vb2_dma_contig_memops;
718 	dst_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
719 			    DMA_ATTR_NO_KERNEL_MAPPING;
720 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
721 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
722 	dst_vq->drv_priv = ctx;
723 	dst_vq->ops = &rkvdec_queue_ops;
724 	dst_vq->buf_struct_size = sizeof(struct rkvdec_decoded_buffer);
725 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
726 	dst_vq->lock = &rkvdec->vdev_lock;
727 	dst_vq->dev = rkvdec->v4l2_dev.dev;
728 
729 	return vb2_queue_init(dst_vq);
730 }
731 
732 static int rkvdec_add_ctrls(struct rkvdec_ctx *ctx,
733 			    const struct rkvdec_ctrls *ctrls)
734 {
735 	unsigned int i;
736 
737 	for (i = 0; i < ctrls->num_ctrls; i++) {
738 		const struct v4l2_ctrl_config *cfg = &ctrls->ctrls[i].cfg;
739 
740 		v4l2_ctrl_new_custom(&ctx->ctrl_hdl, cfg, ctx);
741 		if (ctx->ctrl_hdl.error)
742 			return ctx->ctrl_hdl.error;
743 	}
744 
745 	return 0;
746 }
747 
748 static int rkvdec_init_ctrls(struct rkvdec_ctx *ctx)
749 {
750 	unsigned int i, nctrls = 0;
751 	int ret;
752 
753 	for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++)
754 		nctrls += rkvdec_coded_fmts[i].ctrls->num_ctrls;
755 
756 	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, nctrls);
757 
758 	for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++) {
759 		ret = rkvdec_add_ctrls(ctx, rkvdec_coded_fmts[i].ctrls);
760 		if (ret)
761 			goto err_free_handler;
762 	}
763 
764 	ret = v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
765 	if (ret)
766 		goto err_free_handler;
767 
768 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
769 	return 0;
770 
771 err_free_handler:
772 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
773 	return ret;
774 }
775 
776 static int rkvdec_open(struct file *filp)
777 {
778 	struct rkvdec_dev *rkvdec = video_drvdata(filp);
779 	struct rkvdec_ctx *ctx;
780 	int ret;
781 
782 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
783 	if (!ctx)
784 		return -ENOMEM;
785 
786 	ctx->dev = rkvdec;
787 	rkvdec_reset_coded_fmt(ctx);
788 	rkvdec_reset_decoded_fmt(ctx);
789 	v4l2_fh_init(&ctx->fh, video_devdata(filp));
790 
791 	ret = rkvdec_init_ctrls(ctx);
792 	if (ret)
793 		goto err_free_ctx;
794 
795 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(rkvdec->m2m_dev, ctx,
796 					    rkvdec_queue_init);
797 	if (IS_ERR(ctx->fh.m2m_ctx)) {
798 		ret = PTR_ERR(ctx->fh.m2m_ctx);
799 		goto err_cleanup_ctrls;
800 	}
801 
802 	filp->private_data = &ctx->fh;
803 	v4l2_fh_add(&ctx->fh);
804 
805 	return 0;
806 
807 err_cleanup_ctrls:
808 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
809 
810 err_free_ctx:
811 	kfree(ctx);
812 	return ret;
813 }
814 
815 static int rkvdec_release(struct file *filp)
816 {
817 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(filp->private_data);
818 
819 	v4l2_fh_del(&ctx->fh);
820 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
821 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
822 	v4l2_fh_exit(&ctx->fh);
823 	kfree(ctx);
824 
825 	return 0;
826 }
827 
828 static const struct v4l2_file_operations rkvdec_fops = {
829 	.owner = THIS_MODULE,
830 	.open = rkvdec_open,
831 	.release = rkvdec_release,
832 	.poll = v4l2_m2m_fop_poll,
833 	.unlocked_ioctl = video_ioctl2,
834 	.mmap = v4l2_m2m_fop_mmap,
835 };
836 
837 static int rkvdec_v4l2_init(struct rkvdec_dev *rkvdec)
838 {
839 	int ret;
840 
841 	ret = v4l2_device_register(rkvdec->dev, &rkvdec->v4l2_dev);
842 	if (ret) {
843 		dev_err(rkvdec->dev, "Failed to register V4L2 device\n");
844 		return ret;
845 	}
846 
847 	rkvdec->m2m_dev = v4l2_m2m_init(&rkvdec_m2m_ops);
848 	if (IS_ERR(rkvdec->m2m_dev)) {
849 		v4l2_err(&rkvdec->v4l2_dev, "Failed to init mem2mem device\n");
850 		ret = PTR_ERR(rkvdec->m2m_dev);
851 		goto err_unregister_v4l2;
852 	}
853 
854 	rkvdec->mdev.dev = rkvdec->dev;
855 	strscpy(rkvdec->mdev.model, "rkvdec", sizeof(rkvdec->mdev.model));
856 	strscpy(rkvdec->mdev.bus_info, "platform:rkvdec",
857 		sizeof(rkvdec->mdev.bus_info));
858 	media_device_init(&rkvdec->mdev);
859 	rkvdec->mdev.ops = &rkvdec_media_ops;
860 	rkvdec->v4l2_dev.mdev = &rkvdec->mdev;
861 
862 	rkvdec->vdev.lock = &rkvdec->vdev_lock;
863 	rkvdec->vdev.v4l2_dev = &rkvdec->v4l2_dev;
864 	rkvdec->vdev.fops = &rkvdec_fops;
865 	rkvdec->vdev.release = video_device_release_empty;
866 	rkvdec->vdev.vfl_dir = VFL_DIR_M2M;
867 	rkvdec->vdev.device_caps = V4L2_CAP_STREAMING |
868 				   V4L2_CAP_VIDEO_M2M_MPLANE;
869 	rkvdec->vdev.ioctl_ops = &rkvdec_ioctl_ops;
870 	video_set_drvdata(&rkvdec->vdev, rkvdec);
871 	strscpy(rkvdec->vdev.name, "rkvdec", sizeof(rkvdec->vdev.name));
872 
873 	ret = video_register_device(&rkvdec->vdev, VFL_TYPE_VIDEO, -1);
874 	if (ret) {
875 		v4l2_err(&rkvdec->v4l2_dev, "Failed to register video device\n");
876 		goto err_cleanup_mc;
877 	}
878 
879 	ret = v4l2_m2m_register_media_controller(rkvdec->m2m_dev, &rkvdec->vdev,
880 						 MEDIA_ENT_F_PROC_VIDEO_DECODER);
881 	if (ret) {
882 		v4l2_err(&rkvdec->v4l2_dev,
883 			 "Failed to initialize V4L2 M2M media controller\n");
884 		goto err_unregister_vdev;
885 	}
886 
887 	ret = media_device_register(&rkvdec->mdev);
888 	if (ret) {
889 		v4l2_err(&rkvdec->v4l2_dev, "Failed to register media device\n");
890 		goto err_unregister_mc;
891 	}
892 
893 	return 0;
894 
895 err_unregister_mc:
896 	v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
897 
898 err_unregister_vdev:
899 	video_unregister_device(&rkvdec->vdev);
900 
901 err_cleanup_mc:
902 	media_device_cleanup(&rkvdec->mdev);
903 	v4l2_m2m_release(rkvdec->m2m_dev);
904 
905 err_unregister_v4l2:
906 	v4l2_device_unregister(&rkvdec->v4l2_dev);
907 	return ret;
908 }
909 
910 static void rkvdec_v4l2_cleanup(struct rkvdec_dev *rkvdec)
911 {
912 	media_device_unregister(&rkvdec->mdev);
913 	v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
914 	video_unregister_device(&rkvdec->vdev);
915 	media_device_cleanup(&rkvdec->mdev);
916 	v4l2_m2m_release(rkvdec->m2m_dev);
917 	v4l2_device_unregister(&rkvdec->v4l2_dev);
918 }
919 
920 static irqreturn_t rkvdec_irq_handler(int irq, void *priv)
921 {
922 	struct rkvdec_dev *rkvdec = priv;
923 	enum vb2_buffer_state state;
924 	u32 status;
925 
926 	status = readl(rkvdec->regs + RKVDEC_REG_INTERRUPT);
927 	state = (status & RKVDEC_RDY_STA) ?
928 		VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
929 
930 	writel(0, rkvdec->regs + RKVDEC_REG_INTERRUPT);
931 	if (cancel_delayed_work(&rkvdec->watchdog_work)) {
932 		struct rkvdec_ctx *ctx;
933 
934 		ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
935 		rkvdec_job_finish(ctx, state);
936 	}
937 
938 	return IRQ_HANDLED;
939 }
940 
941 static void rkvdec_watchdog_func(struct work_struct *work)
942 {
943 	struct rkvdec_dev *rkvdec;
944 	struct rkvdec_ctx *ctx;
945 
946 	rkvdec = container_of(to_delayed_work(work), struct rkvdec_dev,
947 			      watchdog_work);
948 	ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
949 	if (ctx) {
950 		dev_err(rkvdec->dev, "Frame processing timed out!\n");
951 		writel(RKVDEC_IRQ_DIS, rkvdec->regs + RKVDEC_REG_INTERRUPT);
952 		writel(0, rkvdec->regs + RKVDEC_REG_SYSCTRL);
953 		rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
954 	}
955 }
956 
957 static const struct of_device_id of_rkvdec_match[] = {
958 	{ .compatible = "rockchip,rk3399-vdec" },
959 	{ /* sentinel */ }
960 };
961 MODULE_DEVICE_TABLE(of, of_rkvdec_match);
962 
963 static const char * const rkvdec_clk_names[] = {
964 	"axi", "ahb", "cabac", "core"
965 };
966 
967 static int rkvdec_probe(struct platform_device *pdev)
968 {
969 	struct rkvdec_dev *rkvdec;
970 	unsigned int i;
971 	int ret, irq;
972 
973 	rkvdec = devm_kzalloc(&pdev->dev, sizeof(*rkvdec), GFP_KERNEL);
974 	if (!rkvdec)
975 		return -ENOMEM;
976 
977 	platform_set_drvdata(pdev, rkvdec);
978 	rkvdec->dev = &pdev->dev;
979 	mutex_init(&rkvdec->vdev_lock);
980 	INIT_DELAYED_WORK(&rkvdec->watchdog_work, rkvdec_watchdog_func);
981 
982 	rkvdec->clocks = devm_kcalloc(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
983 				      sizeof(*rkvdec->clocks), GFP_KERNEL);
984 	if (!rkvdec->clocks)
985 		return -ENOMEM;
986 
987 	for (i = 0; i < ARRAY_SIZE(rkvdec_clk_names); i++)
988 		rkvdec->clocks[i].id = rkvdec_clk_names[i];
989 
990 	ret = devm_clk_bulk_get(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
991 				rkvdec->clocks);
992 	if (ret)
993 		return ret;
994 
995 	/*
996 	 * Bump ACLK to max. possible freq. (500 MHz) to improve performance
997 	 * When 4k video playback.
998 	 */
999 	clk_set_rate(rkvdec->clocks[0].clk, 500 * 1000 * 1000);
1000 
1001 	rkvdec->regs = devm_platform_ioremap_resource(pdev, 0);
1002 	if (IS_ERR(rkvdec->regs))
1003 		return PTR_ERR(rkvdec->regs);
1004 
1005 	ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1006 	if (ret) {
1007 		dev_err(&pdev->dev, "Could not set DMA coherent mask.\n");
1008 		return ret;
1009 	}
1010 
1011 	vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
1012 
1013 	irq = platform_get_irq(pdev, 0);
1014 	if (irq <= 0)
1015 		return -ENXIO;
1016 
1017 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1018 					rkvdec_irq_handler, IRQF_ONESHOT,
1019 					dev_name(&pdev->dev), rkvdec);
1020 	if (ret) {
1021 		dev_err(&pdev->dev, "Could not request vdec IRQ\n");
1022 		return ret;
1023 	}
1024 
1025 	pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
1026 	pm_runtime_use_autosuspend(&pdev->dev);
1027 	pm_runtime_enable(&pdev->dev);
1028 
1029 	ret = rkvdec_v4l2_init(rkvdec);
1030 	if (ret)
1031 		goto err_disable_runtime_pm;
1032 
1033 	return 0;
1034 
1035 err_disable_runtime_pm:
1036 	pm_runtime_dont_use_autosuspend(&pdev->dev);
1037 	pm_runtime_disable(&pdev->dev);
1038 	return ret;
1039 }
1040 
1041 static int rkvdec_remove(struct platform_device *pdev)
1042 {
1043 	struct rkvdec_dev *rkvdec = platform_get_drvdata(pdev);
1044 
1045 	rkvdec_v4l2_cleanup(rkvdec);
1046 	pm_runtime_disable(&pdev->dev);
1047 	pm_runtime_dont_use_autosuspend(&pdev->dev);
1048 	return 0;
1049 }
1050 
1051 #ifdef CONFIG_PM
1052 static int rkvdec_runtime_resume(struct device *dev)
1053 {
1054 	struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
1055 
1056 	return clk_bulk_prepare_enable(ARRAY_SIZE(rkvdec_clk_names),
1057 				       rkvdec->clocks);
1058 }
1059 
1060 static int rkvdec_runtime_suspend(struct device *dev)
1061 {
1062 	struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
1063 
1064 	clk_bulk_disable_unprepare(ARRAY_SIZE(rkvdec_clk_names),
1065 				   rkvdec->clocks);
1066 	return 0;
1067 }
1068 #endif
1069 
1070 static const struct dev_pm_ops rkvdec_pm_ops = {
1071 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1072 				pm_runtime_force_resume)
1073 	SET_RUNTIME_PM_OPS(rkvdec_runtime_suspend, rkvdec_runtime_resume, NULL)
1074 };
1075 
1076 static struct platform_driver rkvdec_driver = {
1077 	.probe = rkvdec_probe,
1078 	.remove = rkvdec_remove,
1079 	.driver = {
1080 		   .name = "rkvdec",
1081 		   .of_match_table = of_rkvdec_match,
1082 		   .pm = &rkvdec_pm_ops,
1083 	},
1084 };
1085 module_platform_driver(rkvdec_driver);
1086 
1087 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@collabora.com>");
1088 MODULE_DESCRIPTION("Rockchip Video Decoder driver");
1089 MODULE_LICENSE("GPL v2");
1090