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