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