xref: /linux/drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp8_req_if.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2021 MediaTek Inc.
4  * Author: Yunfei Dong <yunfei.dong@mediatek.com>
5  */
6 
7 #include <linux/slab.h>
8 #include <media/v4l2-mem2mem.h>
9 #include <media/videobuf2-dma-contig.h>
10 #include <uapi/linux/v4l2-controls.h>
11 
12 #include "../mtk_vcodec_dec.h"
13 #include "../../common/mtk_vcodec_intr.h"
14 #include "../vdec_drv_base.h"
15 #include "../vdec_drv_if.h"
16 #include "../vdec_vpu_if.h"
17 
18 /* Decoding picture buffer size (3 reference frames plus current frame) */
19 #define VP8_DPB_SIZE 4
20 
21 /* HW working buffer size (bytes) */
22 #define VP8_SEG_ID_SZ   SZ_256K
23 #define VP8_PP_WRAPY_SZ SZ_64K
24 #define VP8_PP_WRAPC_SZ SZ_64K
25 #define VP8_VLD_PRED_SZ SZ_64K
26 
27 /**
28  * struct vdec_vp8_slice_info - decode misc information
29  *
30  * @vld_wrapper_dma:	vld wrapper dma address
31  * @seg_id_buf_dma:	seg id dma address
32  * @wrap_y_dma:	wrap y dma address
33  * @wrap_c_dma:	wrap y dma address
34  * @cur_y_fb_dma:	current plane Y frame buffer dma address
35  * @cur_c_fb_dma:	current plane C frame buffer dma address
36  * @bs_dma:		bitstream dma address
37  * @bs_sz:		bitstream size
38  * @resolution_changed:resolution change flag 1 - changed,  0 - not changed
39  * @frame_header_type:	current frame header type
40  * @crc:		used to check whether hardware's status is right
41  * @reserved:		reserved, currently unused
42  */
43 struct vdec_vp8_slice_info {
44 	u64 vld_wrapper_dma;
45 	u64 seg_id_buf_dma;
46 	u64 wrap_y_dma;
47 	u64 wrap_c_dma;
48 	u64 cur_y_fb_dma;
49 	u64 cur_c_fb_dma;
50 	u64 bs_dma;
51 	u32 bs_sz;
52 	u32 resolution_changed;
53 	u32 frame_header_type;
54 	u32 crc[8];
55 	u32 reserved;
56 };
57 
58 /**
59  * struct vdec_vp8_slice_dpb_info  - vp8 reference information
60  *
61  * @y_dma_addr:	Y bitstream physical address
62  * @c_dma_addr:	CbCr bitstream physical address
63  * @reference_flag:	reference picture flag
64  * @reserved:		64bit align
65  */
66 struct vdec_vp8_slice_dpb_info {
67 	dma_addr_t y_dma_addr;
68 	dma_addr_t c_dma_addr;
69 	int reference_flag;
70 	int reserved;
71 };
72 
73 /**
74  * struct vdec_vp8_slice_vsi - VPU shared information
75  *
76  * @dec:		decoding information
77  * @pic:		picture information
78  * @vp8_dpb_info:	reference buffer information
79  */
80 struct vdec_vp8_slice_vsi {
81 	struct vdec_vp8_slice_info dec;
82 	struct vdec_pic_info pic;
83 	struct vdec_vp8_slice_dpb_info vp8_dpb_info[3];
84 };
85 
86 /**
87  * struct vdec_vp8_slice_inst - VP8 decoder instance
88  *
89  * @seg_id_buf:	seg buffer
90  * @wrap_y_buf:	wrapper y buffer
91  * @wrap_c_buf:	wrapper c buffer
92  * @vld_wrapper_buf:	vld wrapper buffer
93  * @ctx:		V4L2 context
94  * @vpu:		VPU instance for decoder
95  * @vsi:		VPU share information
96  */
97 struct vdec_vp8_slice_inst {
98 	struct mtk_vcodec_mem seg_id_buf;
99 	struct mtk_vcodec_mem wrap_y_buf;
100 	struct mtk_vcodec_mem wrap_c_buf;
101 	struct mtk_vcodec_mem vld_wrapper_buf;
102 	struct mtk_vcodec_dec_ctx *ctx;
103 	struct vdec_vpu_inst vpu;
104 	struct vdec_vp8_slice_vsi *vsi;
105 };
106 
vdec_vp8_slice_get_ctrl_ptr(struct mtk_vcodec_dec_ctx * ctx,int id)107 static void *vdec_vp8_slice_get_ctrl_ptr(struct mtk_vcodec_dec_ctx *ctx, int id)
108 {
109 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, id);
110 
111 	if (!ctrl)
112 		return ERR_PTR(-EINVAL);
113 
114 	return ctrl->p_cur.p;
115 }
116 
vdec_vp8_slice_get_pic_info(struct vdec_vp8_slice_inst * inst)117 static void vdec_vp8_slice_get_pic_info(struct vdec_vp8_slice_inst *inst)
118 {
119 	struct mtk_vcodec_dec_ctx *ctx = inst->ctx;
120 	unsigned int data[3];
121 
122 	data[0] = ctx->picinfo.pic_w;
123 	data[1] = ctx->picinfo.pic_h;
124 	data[2] = ctx->capture_fourcc;
125 	vpu_dec_get_param(&inst->vpu, data, 3, GET_PARAM_PIC_INFO);
126 
127 	ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, 64);
128 	ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, 64);
129 	ctx->picinfo.fb_sz[0] = inst->vpu.fb_sz[0];
130 	ctx->picinfo.fb_sz[1] = inst->vpu.fb_sz[1];
131 
132 	inst->vsi->pic.pic_w = ctx->picinfo.pic_w;
133 	inst->vsi->pic.pic_h = ctx->picinfo.pic_h;
134 	inst->vsi->pic.buf_w = ctx->picinfo.buf_w;
135 	inst->vsi->pic.buf_h = ctx->picinfo.buf_h;
136 	inst->vsi->pic.fb_sz[0] = ctx->picinfo.fb_sz[0];
137 	inst->vsi->pic.fb_sz[1] = ctx->picinfo.fb_sz[1];
138 	mtk_vdec_debug(inst->ctx, "pic(%d, %d), buf(%d, %d)",
139 		       ctx->picinfo.pic_w, ctx->picinfo.pic_h,
140 		       ctx->picinfo.buf_w, ctx->picinfo.buf_h);
141 	mtk_vdec_debug(inst->ctx, "fb size: Y(%d), C(%d)",
142 		       ctx->picinfo.fb_sz[0], ctx->picinfo.fb_sz[1]);
143 }
144 
vdec_vp8_slice_alloc_working_buf(struct vdec_vp8_slice_inst * inst)145 static int vdec_vp8_slice_alloc_working_buf(struct vdec_vp8_slice_inst *inst)
146 {
147 	int err;
148 	struct mtk_vcodec_mem *mem;
149 
150 	mem = &inst->seg_id_buf;
151 	mem->size = VP8_SEG_ID_SZ;
152 	err = mtk_vcodec_mem_alloc(inst->ctx, mem);
153 	if (err) {
154 		mtk_vdec_err(inst->ctx, "Cannot allocate working buffer");
155 		return err;
156 	}
157 	inst->vsi->dec.seg_id_buf_dma = (u64)mem->dma_addr;
158 
159 	mem = &inst->wrap_y_buf;
160 	mem->size = VP8_PP_WRAPY_SZ;
161 	err = mtk_vcodec_mem_alloc(inst->ctx, mem);
162 	if (err) {
163 		mtk_vdec_err(inst->ctx, "cannot allocate WRAP Y buffer");
164 		return err;
165 	}
166 	inst->vsi->dec.wrap_y_dma = (u64)mem->dma_addr;
167 
168 	mem = &inst->wrap_c_buf;
169 	mem->size = VP8_PP_WRAPC_SZ;
170 	err = mtk_vcodec_mem_alloc(inst->ctx, mem);
171 	if (err) {
172 		mtk_vdec_err(inst->ctx, "cannot allocate WRAP C buffer");
173 		return err;
174 	}
175 	inst->vsi->dec.wrap_c_dma = (u64)mem->dma_addr;
176 
177 	mem = &inst->vld_wrapper_buf;
178 	mem->size = VP8_VLD_PRED_SZ;
179 	err = mtk_vcodec_mem_alloc(inst->ctx, mem);
180 	if (err) {
181 		mtk_vdec_err(inst->ctx, "cannot allocate vld wrapper buffer");
182 		return err;
183 	}
184 	inst->vsi->dec.vld_wrapper_dma = (u64)mem->dma_addr;
185 
186 	return 0;
187 }
188 
vdec_vp8_slice_free_working_buf(struct vdec_vp8_slice_inst * inst)189 static void vdec_vp8_slice_free_working_buf(struct vdec_vp8_slice_inst *inst)
190 {
191 	struct mtk_vcodec_mem *mem;
192 
193 	mem = &inst->seg_id_buf;
194 	if (mem->va)
195 		mtk_vcodec_mem_free(inst->ctx, mem);
196 	inst->vsi->dec.seg_id_buf_dma = 0;
197 
198 	mem = &inst->wrap_y_buf;
199 	if (mem->va)
200 		mtk_vcodec_mem_free(inst->ctx, mem);
201 	inst->vsi->dec.wrap_y_dma = 0;
202 
203 	mem = &inst->wrap_c_buf;
204 	if (mem->va)
205 		mtk_vcodec_mem_free(inst->ctx, mem);
206 	inst->vsi->dec.wrap_c_dma = 0;
207 
208 	mem = &inst->vld_wrapper_buf;
209 	if (mem->va)
210 		mtk_vcodec_mem_free(inst->ctx, mem);
211 	inst->vsi->dec.vld_wrapper_dma = 0;
212 }
213 
vdec_vp8_slice_get_ref_by_ts(const struct v4l2_ctrl_vp8_frame * frame_header,int index)214 static u64 vdec_vp8_slice_get_ref_by_ts(const struct v4l2_ctrl_vp8_frame *frame_header,
215 					int index)
216 {
217 	switch (index) {
218 	case 0:
219 		return frame_header->last_frame_ts;
220 	case 1:
221 		return frame_header->golden_frame_ts;
222 	case 2:
223 		return frame_header->alt_frame_ts;
224 	default:
225 		break;
226 	}
227 
228 	return -1;
229 }
230 
vdec_vp8_slice_get_decode_parameters(struct vdec_vp8_slice_inst * inst)231 static int vdec_vp8_slice_get_decode_parameters(struct vdec_vp8_slice_inst *inst)
232 {
233 	const struct v4l2_ctrl_vp8_frame *frame_header;
234 	struct mtk_vcodec_dec_ctx *ctx = inst->ctx;
235 	struct vb2_queue *vq;
236 	struct vb2_buffer *vb;
237 	u64 referenct_ts;
238 	int index;
239 
240 	frame_header = vdec_vp8_slice_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_VP8_FRAME);
241 	if (IS_ERR(frame_header))
242 		return PTR_ERR(frame_header);
243 
244 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
245 	for (index = 0; index < 3; index++) {
246 		referenct_ts = vdec_vp8_slice_get_ref_by_ts(frame_header, index);
247 		vb = vb2_find_buffer(vq, referenct_ts);
248 		if (!vb) {
249 			if (!V4L2_VP8_FRAME_IS_KEY_FRAME(frame_header))
250 				mtk_vdec_err(inst->ctx, "reference invalid: index(%d) ts(%lld)",
251 					     index, referenct_ts);
252 			inst->vsi->vp8_dpb_info[index].reference_flag = 0;
253 			continue;
254 		}
255 		inst->vsi->vp8_dpb_info[index].reference_flag = 1;
256 
257 		inst->vsi->vp8_dpb_info[index].y_dma_addr =
258 			vb2_dma_contig_plane_dma_addr(vb, 0);
259 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
260 			inst->vsi->vp8_dpb_info[index].c_dma_addr =
261 				vb2_dma_contig_plane_dma_addr(vb, 1);
262 		else
263 			inst->vsi->vp8_dpb_info[index].c_dma_addr =
264 				inst->vsi->vp8_dpb_info[index].y_dma_addr +
265 				ctx->picinfo.fb_sz[0];
266 	}
267 
268 	inst->vsi->dec.frame_header_type = frame_header->flags >> 1;
269 
270 	return 0;
271 }
272 
vdec_vp8_slice_init(struct mtk_vcodec_dec_ctx * ctx)273 static int vdec_vp8_slice_init(struct mtk_vcodec_dec_ctx *ctx)
274 {
275 	struct vdec_vp8_slice_inst *inst;
276 	int err;
277 
278 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
279 	if (!inst)
280 		return -ENOMEM;
281 
282 	inst->ctx = ctx;
283 
284 	inst->vpu.id = SCP_IPI_VDEC_LAT;
285 	inst->vpu.core_id = SCP_IPI_VDEC_CORE;
286 	inst->vpu.ctx = ctx;
287 	inst->vpu.codec_type = ctx->current_codec;
288 	inst->vpu.capture_type = ctx->capture_fourcc;
289 
290 	err = vpu_dec_init(&inst->vpu);
291 	if (err) {
292 		mtk_vdec_err(ctx, "vdec_vp8 init err=%d", err);
293 		goto error_free_inst;
294 	}
295 
296 	inst->vsi = inst->vpu.vsi;
297 	err = vdec_vp8_slice_alloc_working_buf(inst);
298 	if (err)
299 		goto error_deinit;
300 
301 	mtk_vdec_debug(ctx, "vp8 struct size = %d vsi: %d\n",
302 		       (int)sizeof(struct v4l2_ctrl_vp8_frame),
303 		       (int)sizeof(struct vdec_vp8_slice_vsi));
304 	mtk_vdec_debug(ctx, "vp8:%p, codec_type = 0x%x vsi: 0x%p",
305 		       inst, inst->vpu.codec_type, inst->vpu.vsi);
306 
307 	ctx->drv_handle = inst;
308 	return 0;
309 
310 error_deinit:
311 	vpu_dec_deinit(&inst->vpu);
312 error_free_inst:
313 	kfree(inst);
314 	return err;
315 }
316 
vdec_vp8_slice_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)317 static int vdec_vp8_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
318 				 struct vdec_fb *fb, bool *res_chg)
319 {
320 	struct vdec_vp8_slice_inst *inst = h_vdec;
321 	struct vdec_vpu_inst *vpu = &inst->vpu;
322 	struct mtk_video_dec_buf *src_buf_info, *dst_buf_info;
323 	unsigned int data;
324 	u64 y_fb_dma, c_fb_dma;
325 	int err, timeout;
326 
327 	/* Resolution changes are never initiated by us */
328 	*res_chg = false;
329 
330 	/* bs NULL means flush decoder */
331 	if (!bs)
332 		return vpu_dec_reset(vpu);
333 
334 	src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
335 
336 	fb = inst->ctx->dev->vdec_pdata->get_cap_buffer(inst->ctx);
337 	if (!fb) {
338 		mtk_vdec_err(inst->ctx, "fb buffer is NULL");
339 		return -ENOMEM;
340 	}
341 
342 	dst_buf_info = container_of(fb, struct mtk_video_dec_buf, frame_buffer);
343 	y_fb_dma = fb->base_y.dma_addr;
344 	if (inst->ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1)
345 		c_fb_dma = y_fb_dma +
346 			inst->ctx->picinfo.buf_w * inst->ctx->picinfo.buf_h;
347 	else
348 		c_fb_dma = fb->base_c.dma_addr;
349 
350 	inst->vsi->dec.bs_dma = (u64)bs->dma_addr;
351 	inst->vsi->dec.bs_sz = bs->size;
352 	inst->vsi->dec.cur_y_fb_dma = y_fb_dma;
353 	inst->vsi->dec.cur_c_fb_dma = c_fb_dma;
354 
355 	mtk_vdec_debug(inst->ctx, "frame[%d] bs(%zu 0x%llx) y/c(0x%llx 0x%llx)",
356 		       inst->ctx->decoded_frame_cnt,
357 		       bs->size, (u64)bs->dma_addr,
358 		       y_fb_dma, c_fb_dma);
359 
360 	v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb,
361 				   &dst_buf_info->m2m_buf.vb, true);
362 
363 	err = vdec_vp8_slice_get_decode_parameters(inst);
364 	if (err)
365 		goto error;
366 
367 	err = vpu_dec_start(vpu, &data, 1);
368 	if (err) {
369 		mtk_vdec_debug(inst->ctx, "vp8 dec start err!");
370 		goto error;
371 	}
372 
373 	if (inst->vsi->dec.resolution_changed) {
374 		mtk_vdec_debug(inst->ctx, "- resolution_changed -");
375 		*res_chg = true;
376 		return 0;
377 	}
378 
379 	/* wait decode done interrupt */
380 	timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
381 					       50, MTK_VDEC_CORE);
382 
383 	err = vpu_dec_end(vpu);
384 	if (err || timeout)
385 		mtk_vdec_debug(inst->ctx, "vp8 dec error timeout:%d err: %d pic_%d",
386 			       timeout, err, inst->ctx->decoded_frame_cnt);
387 
388 	mtk_vdec_debug(inst->ctx, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
389 		       inst->ctx->decoded_frame_cnt,
390 		       inst->vsi->dec.crc[0], inst->vsi->dec.crc[1],
391 		       inst->vsi->dec.crc[2], inst->vsi->dec.crc[3],
392 		       inst->vsi->dec.crc[4], inst->vsi->dec.crc[5],
393 		       inst->vsi->dec.crc[6], inst->vsi->dec.crc[7]);
394 
395 	inst->ctx->decoded_frame_cnt++;
396 error:
397 	return err;
398 }
399 
vdec_vp8_slice_get_param(void * h_vdec,enum vdec_get_param_type type,void * out)400 static int vdec_vp8_slice_get_param(void *h_vdec, enum vdec_get_param_type type, void *out)
401 {
402 	struct vdec_vp8_slice_inst *inst = h_vdec;
403 
404 	switch (type) {
405 	case GET_PARAM_PIC_INFO:
406 		vdec_vp8_slice_get_pic_info(inst);
407 		break;
408 	case GET_PARAM_CROP_INFO:
409 		mtk_vdec_debug(inst->ctx, "No need to get vp8 crop information.");
410 		break;
411 	case GET_PARAM_DPB_SIZE:
412 		*((unsigned int *)out) = VP8_DPB_SIZE;
413 		break;
414 	default:
415 		mtk_vdec_err(inst->ctx, "invalid get parameter type=%d", type);
416 		return -EINVAL;
417 	}
418 
419 	return 0;
420 }
421 
vdec_vp8_slice_deinit(void * h_vdec)422 static void vdec_vp8_slice_deinit(void *h_vdec)
423 {
424 	struct vdec_vp8_slice_inst *inst = h_vdec;
425 
426 	vpu_dec_deinit(&inst->vpu);
427 	vdec_vp8_slice_free_working_buf(inst);
428 	kfree(inst);
429 }
430 
431 const struct vdec_common_if vdec_vp8_slice_if = {
432 	.init		= vdec_vp8_slice_init,
433 	.decode		= vdec_vp8_slice_decode,
434 	.get_param	= vdec_vp8_slice_get_param,
435 	.deinit		= vdec_vp8_slice_deinit,
436 };
437