GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / media / platform / mediatek / vcodec / mtk_vcodec_dec_stateless.c
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 static struct mtk_codec_framesizes mtk_vdec_framesizes[3];
116
117 static struct mtk_video_fmt default_out_format;
118 static struct mtk_video_fmt default_cap_format;
119 static unsigned int num_formats;
120 static unsigned int num_framesizes;
121
122 static struct v4l2_frmsize_stepwise stepwise_fhd = {
123         .min_width = MTK_VDEC_MIN_W,
124         .max_width = MTK_VDEC_MAX_W,
125         .step_width = 16,
126         .min_height = MTK_VDEC_MIN_H,
127         .max_height = MTK_VDEC_MAX_H,
128         .step_height = 16
129 };
130
131 static void mtk_vdec_stateless_cap_to_disp(struct mtk_vcodec_ctx *ctx, int error,
132                                            struct media_request *src_buf_req)
133 {
134         struct vb2_v4l2_buffer *vb2_dst;
135         enum vb2_buffer_state state;
136
137         if (error)
138                 state = VB2_BUF_STATE_ERROR;
139         else
140                 state = VB2_BUF_STATE_DONE;
141
142         vb2_dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
143         v4l2_m2m_buf_done(vb2_dst, state);
144
145         mtk_v4l2_debug(2, "free frame buffer id:%d to done list",
146                        vb2_dst->vb2_buf.index);
147
148         if (src_buf_req)
149                 v4l2_ctrl_request_complete(src_buf_req, &ctx->ctrl_hdl);
150 }
151
152 static struct vdec_fb *vdec_get_cap_buffer(struct mtk_vcodec_ctx *ctx)
153 {
154         struct mtk_video_dec_buf *framebuf;
155         struct vb2_v4l2_buffer *vb2_v4l2;
156         struct vb2_buffer *dst_buf;
157         struct vdec_fb *pfb;
158
159         vb2_v4l2 = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
160         if (!vb2_v4l2) {
161                 mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
162                 return NULL;
163         }
164
165         dst_buf = &vb2_v4l2->vb2_buf;
166         framebuf = container_of(vb2_v4l2, struct mtk_video_dec_buf, m2m_buf.vb);
167
168         pfb = &framebuf->frame_buffer;
169         pfb->base_y.va = vb2_plane_vaddr(dst_buf, 0);
170         pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
171         pfb->base_y.size = ctx->q_data[MTK_Q_DATA_DST].sizeimage[0];
172
173         if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2) {
174                 pfb->base_c.va = vb2_plane_vaddr(dst_buf, 1);
175                 pfb->base_c.dma_addr =
176                         vb2_dma_contig_plane_dma_addr(dst_buf, 1);
177                 pfb->base_c.size = ctx->q_data[MTK_Q_DATA_DST].sizeimage[1];
178         }
179         mtk_v4l2_debug(1, "id=%d Framebuf  pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx frame_count = %d",
180                        dst_buf->index, pfb, pfb->base_y.va, &pfb->base_y.dma_addr,
181                        &pfb->base_c.dma_addr, pfb->base_y.size, ctx->decoded_frame_cnt);
182
183         return pfb;
184 }
185
186 static void vb2ops_vdec_buf_request_complete(struct vb2_buffer *vb)
187 {
188         struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
189
190         v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_hdl);
191 }
192
193 static void mtk_vdec_worker(struct work_struct *work)
194 {
195         struct mtk_vcodec_ctx *ctx =
196                 container_of(work, struct mtk_vcodec_ctx, decode_work);
197         struct mtk_vcodec_dev *dev = ctx->dev;
198         struct vb2_v4l2_buffer *vb2_v4l2_src;
199         struct vb2_buffer *vb2_src;
200         struct mtk_vcodec_mem *bs_src;
201         struct mtk_video_dec_buf *dec_buf_src;
202         struct media_request *src_buf_req;
203         enum vb2_buffer_state state;
204         bool res_chg = false;
205         int ret;
206
207         vb2_v4l2_src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
208         if (!vb2_v4l2_src) {
209                 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
210                 mtk_v4l2_debug(1, "[%d] no available source buffer", ctx->id);
211                 return;
212         }
213
214         vb2_src = &vb2_v4l2_src->vb2_buf;
215         dec_buf_src = container_of(vb2_v4l2_src, struct mtk_video_dec_buf,
216                                    m2m_buf.vb);
217         bs_src = &dec_buf_src->bs_buffer;
218
219         mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p", ctx->id,
220                        vb2_src->vb2_queue->type, vb2_src->index, vb2_src);
221
222         bs_src->va = vb2_plane_vaddr(vb2_src, 0);
223         bs_src->dma_addr = vb2_dma_contig_plane_dma_addr(vb2_src, 0);
224         bs_src->size = (size_t)vb2_src->planes[0].bytesused;
225         if (!bs_src->va) {
226                 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
227                 mtk_v4l2_err("[%d] id=%d source buffer is NULL", ctx->id,
228                              vb2_src->index);
229                 return;
230         }
231
232         mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
233                        ctx->id, bs_src->va, &bs_src->dma_addr, bs_src->size, vb2_src);
234         /* Apply request controls. */
235         src_buf_req = vb2_src->req_obj.req;
236         if (src_buf_req)
237                 v4l2_ctrl_request_setup(src_buf_req, &ctx->ctrl_hdl);
238         else
239                 mtk_v4l2_err("vb2 buffer media request is NULL");
240
241         ret = vdec_if_decode(ctx, bs_src, NULL, &res_chg);
242         if (ret) {
243                 mtk_v4l2_err(" <===[%d], src_buf[%d] sz=0x%zx pts=%llu vdec_if_decode() ret=%d res_chg=%d===>",
244                              ctx->id, vb2_src->index, bs_src->size,
245                              vb2_src->timestamp, ret, res_chg);
246                 if (ret == -EIO) {
247                         mutex_lock(&ctx->lock);
248                         dec_buf_src->error = true;
249                         mutex_unlock(&ctx->lock);
250                 }
251         }
252
253         state = ret ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE;
254         if (!IS_VDEC_LAT_ARCH(dev->vdec_pdata->hw_arch) ||
255             ctx->current_codec == V4L2_PIX_FMT_VP8_FRAME || ret) {
256                 v4l2_m2m_buf_done_and_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx, state);
257                 if (src_buf_req)
258                         v4l2_ctrl_request_complete(src_buf_req, &ctx->ctrl_hdl);
259         } else {
260                 v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
261                 v4l2_m2m_buf_done(vb2_v4l2_src, state);
262                 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
263         }
264 }
265
266 static void vb2ops_vdec_stateless_buf_queue(struct vb2_buffer *vb)
267 {
268         struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
269         struct vb2_v4l2_buffer *vb2_v4l2 = to_vb2_v4l2_buffer(vb);
270
271         mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p", ctx->id, vb->vb2_queue->type, vb->index, vb);
272
273         mutex_lock(&ctx->lock);
274         v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
275         mutex_unlock(&ctx->lock);
276         if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
277                 return;
278
279         /* If an OUTPUT buffer, we may need to update the state */
280         if (ctx->state == MTK_STATE_INIT) {
281                 ctx->state = MTK_STATE_HEADER;
282                 mtk_v4l2_debug(1, "Init driver from init to header.");
283         } else {
284                 mtk_v4l2_debug(3, "[%d] already init driver %d", ctx->id, ctx->state);
285         }
286 }
287
288 static int mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
289 {
290         bool res_chg;
291
292         return vdec_if_decode(ctx, NULL, NULL, &res_chg);
293 }
294
295 static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
296 {
297         unsigned int i;
298
299         v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS);
300         if (ctx->ctrl_hdl.error) {
301                 mtk_v4l2_err("v4l2_ctrl_handler_init failed\n");
302                 return ctx->ctrl_hdl.error;
303         }
304
305         for (i = 0; i < NUM_CTRLS; i++) {
306                 struct v4l2_ctrl_config cfg = mtk_stateless_controls[i].cfg;
307
308                 v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &cfg, NULL);
309                 if (ctx->ctrl_hdl.error) {
310                         mtk_v4l2_err("Adding control %d failed %d", i, ctx->ctrl_hdl.error);
311                         return ctx->ctrl_hdl.error;
312                 }
313         }
314
315         v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
316
317         return 0;
318 }
319
320 static int fops_media_request_validate(struct media_request *mreq)
321 {
322         const unsigned int buffer_cnt = vb2_request_buffer_cnt(mreq);
323
324         switch (buffer_cnt) {
325         case 1:
326                 /* We expect exactly one buffer with the request */
327                 break;
328         case 0:
329                 mtk_v4l2_debug(1, "No buffer provided with the request");
330                 return -ENOENT;
331         default:
332                 mtk_v4l2_debug(1, "Too many buffers (%d) provided with the request",
333                                buffer_cnt);
334                 return -EINVAL;
335         }
336
337         return vb2_request_validate(mreq);
338 }
339
340 const struct media_device_ops mtk_vcodec_media_ops = {
341         .req_validate   = fops_media_request_validate,
342         .req_queue      = v4l2_m2m_request_queue,
343 };
344
345 static void mtk_vcodec_add_formats(unsigned int fourcc,
346                                    struct mtk_vcodec_ctx *ctx)
347 {
348         struct mtk_vcodec_dev *dev = ctx->dev;
349         const struct mtk_vcodec_dec_pdata *pdata = dev->vdec_pdata;
350         int count_formats = *pdata->num_formats;
351         int count_framesizes = *pdata->num_framesizes;
352
353         switch (fourcc) {
354         case V4L2_PIX_FMT_H264_SLICE:
355         case V4L2_PIX_FMT_VP8_FRAME:
356         case V4L2_PIX_FMT_VP9_FRAME:
357                 mtk_video_formats[count_formats].fourcc = fourcc;
358                 mtk_video_formats[count_formats].type = MTK_FMT_DEC;
359                 mtk_video_formats[count_formats].num_planes = 1;
360
361                 mtk_vdec_framesizes[count_framesizes].fourcc = fourcc;
362                 mtk_vdec_framesizes[count_framesizes].stepwise = stepwise_fhd;
363                 num_framesizes++;
364                 break;
365         case V4L2_PIX_FMT_MM21:
366         case V4L2_PIX_FMT_MT21C:
367                 mtk_video_formats[count_formats].fourcc = fourcc;
368                 mtk_video_formats[count_formats].type = MTK_FMT_FRAME;
369                 mtk_video_formats[count_formats].num_planes = 2;
370                 break;
371         default:
372                 mtk_v4l2_err("Can not add unsupported format type");
373                 return;
374         }
375
376         num_formats++;
377         mtk_v4l2_debug(3, "num_formats: %d num_frames:%d dec_capability: 0x%x",
378                        count_formats, count_framesizes, ctx->dev->dec_capability);
379 }
380
381 static void mtk_vcodec_get_supported_formats(struct mtk_vcodec_ctx *ctx)
382 {
383         int cap_format_count = 0, out_format_count = 0;
384
385         if (num_formats && num_framesizes)
386                 return;
387
388         if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_MM21) {
389                 mtk_vcodec_add_formats(V4L2_PIX_FMT_MM21, ctx);
390                 cap_format_count++;
391         }
392         if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_MT21C) {
393                 mtk_vcodec_add_formats(V4L2_PIX_FMT_MT21C, ctx);
394                 cap_format_count++;
395         }
396         if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_H264_SLICE) {
397                 mtk_vcodec_add_formats(V4L2_PIX_FMT_H264_SLICE, ctx);
398                 out_format_count++;
399         }
400         if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_VP8_FRAME) {
401                 mtk_vcodec_add_formats(V4L2_PIX_FMT_VP8_FRAME, ctx);
402                 out_format_count++;
403         }
404         if (ctx->dev->dec_capability & MTK_VDEC_FORMAT_VP9_FRAME) {
405                 mtk_vcodec_add_formats(V4L2_PIX_FMT_VP9_FRAME, ctx);
406                 out_format_count++;
407         }
408
409         if (cap_format_count)
410                 default_cap_format = mtk_video_formats[cap_format_count - 1];
411         if (out_format_count)
412                 default_out_format =
413                         mtk_video_formats[cap_format_count + out_format_count - 1];
414 }
415
416 static void mtk_init_vdec_params(struct mtk_vcodec_ctx *ctx)
417 {
418         struct vb2_queue *src_vq;
419
420         src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
421                                  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
422
423         if (!ctx->dev->vdec_pdata->is_subdev_supported)
424                 ctx->dev->dec_capability |=
425                         MTK_VDEC_FORMAT_H264_SLICE | MTK_VDEC_FORMAT_MM21;
426         mtk_vcodec_get_supported_formats(ctx);
427
428         /* Support request api for output plane */
429         src_vq->supports_requests = true;
430         src_vq->requires_requests = true;
431 }
432
433 static int vb2ops_vdec_out_buf_validate(struct vb2_buffer *vb)
434 {
435         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
436
437         vbuf->field = V4L2_FIELD_NONE;
438         return 0;
439 }
440
441 static struct vb2_ops mtk_vdec_request_vb2_ops = {
442         .queue_setup    = vb2ops_vdec_queue_setup,
443         .wait_prepare   = vb2_ops_wait_prepare,
444         .wait_finish    = vb2_ops_wait_finish,
445         .start_streaming        = vb2ops_vdec_start_streaming,
446         .stop_streaming = vb2ops_vdec_stop_streaming,
447
448         .buf_queue      = vb2ops_vdec_stateless_buf_queue,
449         .buf_out_validate = vb2ops_vdec_out_buf_validate,
450         .buf_init       = vb2ops_vdec_buf_init,
451         .buf_prepare    = vb2ops_vdec_buf_prepare,
452         .buf_finish     = vb2ops_vdec_buf_finish,
453         .buf_request_complete = vb2ops_vdec_buf_request_complete,
454 };
455
456 const struct mtk_vcodec_dec_pdata mtk_vdec_8183_pdata = {
457         .init_vdec_params = mtk_init_vdec_params,
458         .ctrls_setup = mtk_vcodec_dec_ctrls_setup,
459         .vdec_vb2_ops = &mtk_vdec_request_vb2_ops,
460         .vdec_formats = mtk_video_formats,
461         .num_formats = &num_formats,
462         .default_out_fmt = &default_out_format,
463         .default_cap_fmt = &default_cap_format,
464         .vdec_framesizes = mtk_vdec_framesizes,
465         .num_framesizes = &num_framesizes,
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         .vdec_framesizes = mtk_vdec_framesizes,
485         .num_framesizes = &num_framesizes,
486         .uses_stateless_api = true,
487         .worker = mtk_vdec_worker,
488         .flush_decoder = mtk_vdec_flush_decoder,
489         .cap_to_disp = mtk_vdec_stateless_cap_to_disp,
490         .get_cap_buffer = vdec_get_cap_buffer,
491         .is_subdev_supported = true,
492         .hw_arch = MTK_VDEC_LAT_SINGLE_CORE,
493 };
494
495 const struct mtk_vcodec_dec_pdata mtk_vdec_single_core_pdata = {
496         .init_vdec_params = mtk_init_vdec_params,
497         .ctrls_setup = mtk_vcodec_dec_ctrls_setup,
498         .vdec_vb2_ops = &mtk_vdec_request_vb2_ops,
499         .vdec_formats = mtk_video_formats,
500         .num_formats = &num_formats,
501         .default_out_fmt = &default_out_format,
502         .default_cap_fmt = &default_cap_format,
503         .vdec_framesizes = mtk_vdec_framesizes,
504         .num_framesizes = &num_framesizes,
505         .uses_stateless_api = true,
506         .worker = mtk_vdec_worker,
507         .flush_decoder = mtk_vdec_flush_decoder,
508         .cap_to_disp = mtk_vdec_stateless_cap_to_disp,
509         .get_cap_buffer = vdec_get_cap_buffer,
510         .is_subdev_supported = true,
511         .hw_arch = MTK_VDEC_PURE_SINGLE_CORE,
512 };