1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <media/videobuf2-v4l2.h>
4 #include <media/videobuf2-dma-contig.h>
5 #include <media/v4l2-event.h>
6 #include <media/v4l2-mem2mem.h>
7 #include <linux/module.h>
8
9 #include "mtk_vcodec_drv.h"
10 #include "mtk_vcodec_dec.h"
11 #include "mtk_vcodec_intr.h"
12 #include "mtk_vcodec_util.h"
13 #include "mtk_vcodec_dec_pm.h"
14 #include "vdec_drv_if.h"
15
16 /**
17 * struct mtk_stateless_control - CID control type
18 * @cfg: control configuration
19 * @codec_type: codec type (V4L2 pixel format) for CID control type
20 */
21 struct mtk_stateless_control {
22 struct v4l2_ctrl_config cfg;
23 int codec_type;
24 };
25
26 static const struct mtk_stateless_control mtk_stateless_controls[] = {
27 {
28 .cfg = {
29 .id = V4L2_CID_STATELESS_H264_SPS,
30 },
31 .codec_type = V4L2_PIX_FMT_H264_SLICE,
32 },
33 {
34 .cfg = {
35 .id = V4L2_CID_STATELESS_H264_PPS,
36 },
37 .codec_type = V4L2_PIX_FMT_H264_SLICE,
38 },
39 {
40 .cfg = {
41 .id = V4L2_CID_STATELESS_H264_SCALING_MATRIX,
42 },
43 .codec_type = V4L2_PIX_FMT_H264_SLICE,
44 },
45 {
46 .cfg = {
47 .id = V4L2_CID_STATELESS_H264_DECODE_PARAMS,
48 },
49 .codec_type = V4L2_PIX_FMT_H264_SLICE,
50 },
51 {
52 .cfg = {
53 .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
54 .def = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
55 .max = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
56 .menu_skip_mask =
57 BIT(V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
58 BIT(V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED),
59 },
60 .codec_type = V4L2_PIX_FMT_H264_SLICE,
61 },
62 {
63 .cfg = {
64 .id = V4L2_CID_STATELESS_H264_DECODE_MODE,
65 .min = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
66 .def = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
67 .max = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
68 },
69 .codec_type = V4L2_PIX_FMT_H264_SLICE,
70 },
71 {
72 .cfg = {
73 .id = V4L2_CID_STATELESS_H264_START_CODE,
74 .min = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
75 .def = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
76 .max = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
77 },
78 .codec_type = V4L2_PIX_FMT_H264_SLICE,
79 },
80 {
81 .cfg = {
82 .id = V4L2_CID_STATELESS_VP8_FRAME,
83 },
84 .codec_type = V4L2_PIX_FMT_VP8_FRAME,
85 },
86 {
87 .cfg = {
88 .id = V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
89 .min = V4L2_MPEG_VIDEO_VP8_PROFILE_0,
90 .def = V4L2_MPEG_VIDEO_VP8_PROFILE_0,
91 .max = V4L2_MPEG_VIDEO_VP8_PROFILE_3,
92 },
93 .codec_type = V4L2_PIX_FMT_VP8_FRAME,
94 },
95 {
96 .cfg = {
97 .id = V4L2_CID_STATELESS_VP9_FRAME,
98 },
99 .codec_type = V4L2_PIX_FMT_VP9_FRAME,
100 },
101 {
102 .cfg = {
103 .id = V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
104 .min = V4L2_MPEG_VIDEO_VP9_PROFILE_0,
105 .def = V4L2_MPEG_VIDEO_VP9_PROFILE_0,
106 .max = V4L2_MPEG_VIDEO_VP9_PROFILE_3,
107 },
108 .codec_type = V4L2_PIX_FMT_VP9_FRAME,
109 },
110 };
111
112 #define NUM_CTRLS ARRAY_SIZE(mtk_stateless_controls)
113
114 static struct mtk_video_fmt mtk_video_formats[5];
115
116 static struct mtk_video_fmt default_out_format;
117 static struct mtk_video_fmt default_cap_format;
118 static unsigned int num_formats;
119
120 static const struct v4l2_frmsize_stepwise stepwise_fhd = {
121 .min_width = MTK_VDEC_MIN_W,
122 .max_width = MTK_VDEC_MAX_W,
123 .step_width = 16,
124 .min_height = MTK_VDEC_MIN_H,
125 .max_height = MTK_VDEC_MAX_H,
126 .step_height = 16
127 };
128
mtk_vdec_stateless_cap_to_disp(struct mtk_vcodec_ctx * ctx,int error,struct media_request * src_buf_req)129 static void mtk_vdec_stateless_cap_to_disp(struct mtk_vcodec_ctx *ctx, int error,
130 struct media_request *src_buf_req)
131 {
132 struct vb2_v4l2_buffer *vb2_dst;
133 enum vb2_buffer_state state;
134
135 if (error)
136 state = VB2_BUF_STATE_ERROR;
137 else
138 state = VB2_BUF_STATE_DONE;
139
140 vb2_dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
141 v4l2_m2m_buf_done(vb2_dst, state);
142
143 mtk_v4l2_debug(2, "free frame buffer id:%d to done list",
144 vb2_dst->vb2_buf.index);
145
146 if (src_buf_req)
147 v4l2_ctrl_request_complete(src_buf_req, &ctx->ctrl_hdl);
148 }
149
vdec_get_cap_buffer(struct mtk_vcodec_ctx * ctx)150 static struct vdec_fb *vdec_get_cap_buffer(struct mtk_vcodec_ctx *ctx)
151 {
152 struct mtk_video_dec_buf *framebuf;
153 struct vb2_v4l2_buffer *vb2_v4l2;
154 struct vb2_buffer *dst_buf;
155 struct vdec_fb *pfb;
156
157 vb2_v4l2 = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
158 if (!vb2_v4l2) {
159 mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
160 return NULL;
161 }
162
163 dst_buf = &vb2_v4l2->vb2_buf;
164 framebuf = container_of(vb2_v4l2, struct mtk_video_dec_buf, m2m_buf.vb);
165
166 pfb = &framebuf->frame_buffer;
167 pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
168 pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
169 pfb->base_y.size = ctx->q_data[MTK_Q_DATA_DST].sizeimage[0];
170
171 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2) {
172 pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
173 pfb->base_c.dma_addr =
174 vb2_dma_contig_plane_dma_addr(dst_buf, 1);
175 pfb->base_c.size = ctx->q_data[MTK_Q_DATA_DST].sizeimage[1];
176 }
177 mtk_v4l2_debug(1, "id=%d Framebuf pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx frame_count = %d",
178 dst_buf->index, pfb, pfb->base_y.va, &pfb->base_y.dma_addr,
179 &pfb->base_c.dma_addr, pfb->base_y.size, ctx->decoded_frame_cnt);
180
181 return pfb;
182 }
183
vb2ops_vdec_buf_request_complete(struct vb2_buffer * vb)184 static void vb2ops_vdec_buf_request_complete(struct vb2_buffer *vb)
185 {
186 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
187
188 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_hdl);
189 }
190
mtk_vdec_worker(struct work_struct * work)191 static void mtk_vdec_worker(struct work_struct *work)
192 {
193 struct mtk_vcodec_ctx *ctx =
194 container_of(work, struct mtk_vcodec_ctx, decode_work);
195 struct mtk_vcodec_dev *dev = ctx->dev;
196 struct vb2_v4l2_buffer *vb2_v4l2_src;
197 struct vb2_buffer *vb2_src;
198 struct mtk_vcodec_mem *bs_src;
199 struct mtk_video_dec_buf *dec_buf_src;
200 struct media_request *src_buf_req;
201 enum vb2_buffer_state state;
202 bool res_chg = false;
203 int ret;
204
205 vb2_v4l2_src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
206 if (!vb2_v4l2_src) {
207 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
208 mtk_v4l2_debug(1, "[%d] no available source buffer", ctx->id);
209 return;
210 }
211
212 vb2_src = &vb2_v4l2_src->vb2_buf;
213 dec_buf_src = container_of(vb2_v4l2_src, struct mtk_video_dec_buf,
214 m2m_buf.vb);
215 bs_src = &dec_buf_src->bs_buffer;
216
217 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p", ctx->id,
218 vb2_src->vb2_queue->type, vb2_src->index, vb2_src);
219
220 bs_src->va = vb2_plane_vaddr(vb2_src, 0);
221 bs_src->dma_addr = vb2_dma_contig_plane_dma_addr(vb2_src, 0);
222 bs_src->size = (size_t)vb2_src->planes[0].bytesused;
223 if (!bs_src->va) {
224 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
225 mtk_v4l2_err("[%d] id=%d source buffer is NULL", ctx->id,
226 vb2_src->index);
227 return;
228 }
229
230 mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
231 ctx->id, bs_src->va, &bs_src->dma_addr, bs_src->size, vb2_src);
232 /* Apply request controls. */
233 src_buf_req = vb2_src->req_obj.req;
234 if (src_buf_req)
235 v4l2_ctrl_request_setup(src_buf_req, &ctx->ctrl_hdl);
236 else
237 mtk_v4l2_err("vb2 buffer media request is NULL");
238
239 ret = vdec_if_decode(ctx, bs_src, NULL, &res_chg);
240 if (ret) {
241 mtk_v4l2_err(" <===[%d], src_buf[%d] sz=0x%zx pts=%llu vdec_if_decode() ret=%d res_chg=%d===>",
242 ctx->id, vb2_src->index, bs_src->size,
243 vb2_src->timestamp, ret, res_chg);
244 if (ret == -EIO) {
245 mutex_lock(&ctx->lock);
246 dec_buf_src->error = true;
247 mutex_unlock(&ctx->lock);
248 }
249 }
250
251 state = ret ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE;
252 if (!IS_VDEC_LAT_ARCH(dev->vdec_pdata->hw_arch) ||
253 ctx->current_codec == V4L2_PIX_FMT_VP8_FRAME || ret) {
254 v4l2_m2m_buf_done_and_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx, state);
255 if (src_buf_req)
256 v4l2_ctrl_request_complete(src_buf_req, &ctx->ctrl_hdl);
257 } else {
258 v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
259 v4l2_m2m_buf_done(vb2_v4l2_src, state);
260 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
261 }
262 }
263
vb2ops_vdec_stateless_buf_queue(struct vb2_buffer * vb)264 static void vb2ops_vdec_stateless_buf_queue(struct vb2_buffer *vb)
265 {
266 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
267 struct vb2_v4l2_buffer *vb2_v4l2 = to_vb2_v4l2_buffer(vb);
268
269 mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p", ctx->id, vb->vb2_queue->type, vb->index, vb);
270
271 mutex_lock(&ctx->lock);
272 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
273 mutex_unlock(&ctx->lock);
274 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
275 return;
276
277 /* If an OUTPUT buffer, we may need to update the state */
278 if (ctx->state == MTK_STATE_INIT) {
279 ctx->state = MTK_STATE_HEADER;
280 mtk_v4l2_debug(1, "Init driver from init to header.");
281 } else {
282 mtk_v4l2_debug(3, "[%d] already init driver %d", ctx->id, ctx->state);
283 }
284 }
285
mtk_vdec_flush_decoder(struct mtk_vcodec_ctx * ctx)286 static int mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
287 {
288 bool res_chg;
289
290 return vdec_if_decode(ctx, NULL, NULL, &res_chg);
291 }
292
mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx * ctx)293 static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
294 {
295 unsigned int i;
296
297 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS);
298 if (ctx->ctrl_hdl.error) {
299 mtk_v4l2_err("v4l2_ctrl_handler_init failed\n");
300 return ctx->ctrl_hdl.error;
301 }
302
303 for (i = 0; i < NUM_CTRLS; i++) {
304 struct v4l2_ctrl_config cfg = mtk_stateless_controls[i].cfg;
305
306 v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &cfg, NULL);
307 if (ctx->ctrl_hdl.error) {
308 mtk_v4l2_err("Adding control %d failed %d", i, ctx->ctrl_hdl.error);
309 return ctx->ctrl_hdl.error;
310 }
311 }
312
313 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
314
315 return 0;
316 }
317
fops_media_request_validate(struct media_request * mreq)318 static int fops_media_request_validate(struct media_request *mreq)
319 {
320 const unsigned int buffer_cnt = vb2_request_buffer_cnt(mreq);
321
322 switch (buffer_cnt) {
323 case 1:
324 /* We expect exactly one buffer with the request */
325 break;
326 case 0:
327 mtk_v4l2_debug(1, "No buffer provided with the request");
328 return -ENOENT;
329 default:
330 mtk_v4l2_debug(1, "Too many buffers (%d) provided with the request",
331 buffer_cnt);
332 return -EINVAL;
333 }
334
335 return vb2_request_validate(mreq);
336 }
337
338 const struct media_device_ops mtk_vcodec_media_ops = {
339 .req_validate = fops_media_request_validate,
340 .req_queue = v4l2_m2m_request_queue,
341 };
342
mtk_vcodec_add_formats(unsigned int fourcc,struct mtk_vcodec_ctx * ctx)343 static void mtk_vcodec_add_formats(unsigned int fourcc,
344 struct mtk_vcodec_ctx *ctx)
345 {
346 struct mtk_vcodec_dev *dev = ctx->dev;
347 const struct mtk_vcodec_dec_pdata *pdata = dev->vdec_pdata;
348 int count_formats = *pdata->num_formats;
349
350 switch (fourcc) {
351 case V4L2_PIX_FMT_H264_SLICE:
352 case V4L2_PIX_FMT_VP8_FRAME:
353 case V4L2_PIX_FMT_VP9_FRAME:
354 mtk_video_formats[count_formats].fourcc = fourcc;
355 mtk_video_formats[count_formats].type = MTK_FMT_DEC;
356 mtk_video_formats[count_formats].num_planes = 1;
357 mtk_video_formats[count_formats].frmsize = stepwise_fhd;
358
359 if (!(ctx->dev->dec_capability & VCODEC_CAPABILITY_4K_DISABLED) &&
360 fourcc != V4L2_PIX_FMT_VP8_FRAME) {
361 mtk_video_formats[count_formats].frmsize.max_width =
362 VCODEC_DEC_4K_CODED_WIDTH;
363 mtk_video_formats[count_formats].frmsize.max_height =
364 VCODEC_DEC_4K_CODED_HEIGHT;
365 }
366 break;
367 case V4L2_PIX_FMT_MM21:
368 case V4L2_PIX_FMT_MT21C:
369 mtk_video_formats[count_formats].fourcc = fourcc;
370 mtk_video_formats[count_formats].type = MTK_FMT_FRAME;
371 mtk_video_formats[count_formats].num_planes = 2;
372 break;
373 default:
374 mtk_v4l2_err("Can not add unsupported format type");
375 return;
376 }
377
378 num_formats++;
379 mtk_v4l2_debug(3, "num_formats: %d dec_capability: 0x%x",
380 count_formats, ctx->dev->dec_capability);
381 }
382
mtk_vcodec_get_supported_formats(struct mtk_vcodec_ctx * ctx)383 static void mtk_vcodec_get_supported_formats(struct mtk_vcodec_ctx *ctx)
384 {
385 int cap_format_count = 0, out_format_count = 0;
386
387 if (num_formats)
388 return;
389
390 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_MM21) {
391 mtk_vcodec_add_formats(V4L2_PIX_FMT_MM21, ctx);
392 cap_format_count++;
393 }
394 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_MT21C) {
395 mtk_vcodec_add_formats(V4L2_PIX_FMT_MT21C, ctx);
396 cap_format_count++;
397 }
398 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_H264_SLICE) {
399 mtk_vcodec_add_formats(V4L2_PIX_FMT_H264_SLICE, ctx);
400 out_format_count++;
401 }
402 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_VP8_FRAME) {
403 mtk_vcodec_add_formats(V4L2_PIX_FMT_VP8_FRAME, ctx);
404 out_format_count++;
405 }
406 if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_VP9_FRAME) {
407 mtk_vcodec_add_formats(V4L2_PIX_FMT_VP9_FRAME, ctx);
408 out_format_count++;
409 }
410
411 if (cap_format_count)
412 default_cap_format = mtk_video_formats[cap_format_count - 1];
413 if (out_format_count)
414 default_out_format =
415 mtk_video_formats[cap_format_count + out_format_count - 1];
416 }
417
mtk_init_vdec_params(struct mtk_vcodec_ctx * ctx)418 static void mtk_init_vdec_params(struct mtk_vcodec_ctx *ctx)
419 {
420 struct vb2_queue *src_vq;
421
422 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
423 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
424
425 if (!ctx->dev->vdec_pdata->is_subdev_supported)
426 ctx->dev->dec_capability |=
427 MTK_VDEC_FORMAT_H264_SLICE | MTK_VDEC_FORMAT_MM21;
428 mtk_vcodec_get_supported_formats(ctx);
429
430 /* Support request api for output plane */
431 src_vq->supports_requests = true;
432 src_vq->requires_requests = true;
433 }
434
vb2ops_vdec_out_buf_validate(struct vb2_buffer * vb)435 static int vb2ops_vdec_out_buf_validate(struct vb2_buffer *vb)
436 {
437 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
438
439 vbuf->field = V4L2_FIELD_NONE;
440 return 0;
441 }
442
443 static struct vb2_ops mtk_vdec_request_vb2_ops = {
444 .queue_setup = vb2ops_vdec_queue_setup,
445 .wait_prepare = vb2_ops_wait_prepare,
446 .wait_finish = vb2_ops_wait_finish,
447 .start_streaming = vb2ops_vdec_start_streaming,
448 .stop_streaming = vb2ops_vdec_stop_streaming,
449
450 .buf_queue = vb2ops_vdec_stateless_buf_queue,
451 .buf_out_validate = vb2ops_vdec_out_buf_validate,
452 .buf_init = vb2ops_vdec_buf_init,
453 .buf_prepare = vb2ops_vdec_buf_prepare,
454 .buf_finish = vb2ops_vdec_buf_finish,
455 .buf_request_complete = vb2ops_vdec_buf_request_complete,
456 };
457
458 const struct mtk_vcodec_dec_pdata mtk_vdec_8183_pdata = {
459 .init_vdec_params = mtk_init_vdec_params,
460 .ctrls_setup = mtk_vcodec_dec_ctrls_setup,
461 .vdec_vb2_ops = &mtk_vdec_request_vb2_ops,
462 .vdec_formats = mtk_video_formats,
463 .num_formats = &num_formats,
464 .default_out_fmt = &default_out_format,
465 .default_cap_fmt = &default_cap_format,
466 .uses_stateless_api = true,
467 .worker = mtk_vdec_worker,
468 .flush_decoder = mtk_vdec_flush_decoder,
469 .cap_to_disp = mtk_vdec_stateless_cap_to_disp,
470 .get_cap_buffer = vdec_get_cap_buffer,
471 .is_subdev_supported = false,
472 .hw_arch = MTK_VDEC_PURE_SINGLE_CORE,
473 };
474
475 /* This platform data is used for one lat and one core architecture. */
476 const struct mtk_vcodec_dec_pdata mtk_lat_sig_core_pdata = {
477 .init_vdec_params = mtk_init_vdec_params,
478 .ctrls_setup = mtk_vcodec_dec_ctrls_setup,
479 .vdec_vb2_ops = &mtk_vdec_request_vb2_ops,
480 .vdec_formats = mtk_video_formats,
481 .num_formats = &num_formats,
482 .default_out_fmt = &default_out_format,
483 .default_cap_fmt = &default_cap_format,
484 .uses_stateless_api = true,
485 .worker = mtk_vdec_worker,
486 .flush_decoder = mtk_vdec_flush_decoder,
487 .cap_to_disp = mtk_vdec_stateless_cap_to_disp,
488 .get_cap_buffer = vdec_get_cap_buffer,
489 .is_subdev_supported = true,
490 .hw_arch = MTK_VDEC_LAT_SINGLE_CORE,
491 };
492
493 const struct mtk_vcodec_dec_pdata mtk_vdec_single_core_pdata = {
494 .init_vdec_params = mtk_init_vdec_params,
495 .ctrls_setup = mtk_vcodec_dec_ctrls_setup,
496 .vdec_vb2_ops = &mtk_vdec_request_vb2_ops,
497 .vdec_formats = mtk_video_formats,
498 .num_formats = &num_formats,
499 .default_out_fmt = &default_out_format,
500 .default_cap_fmt = &default_cap_format,
501 .uses_stateless_api = true,
502 .worker = mtk_vdec_worker,
503 .flush_decoder = mtk_vdec_flush_decoder,
504 .cap_to_disp = mtk_vdec_stateless_cap_to_disp,
505 .get_cap_buffer = vdec_get_cap_buffer,
506 .is_subdev_supported = true,
507 .hw_arch = MTK_VDEC_PURE_SINGLE_CORE,
508 };
509