1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 */
6
7#include <linux/module.h>
8#include <linux/slab.h>
9
10#include "../vdec_drv_if.h"
11#include "../mtk_vcodec_dec.h"
12#include "../../common/mtk_vcodec_intr.h"
13#include "../vdec_vpu_if.h"
14#include "../vdec_drv_base.h"
15
16#define NAL_NON_IDR_SLICE			0x01
17#define NAL_IDR_SLICE				0x05
18#define NAL_H264_PPS				0x08
19#define NAL_TYPE(value)				((value) & 0x1F)
20
21#define BUF_PREDICTION_SZ			(32 * 1024)
22
23#define MB_UNIT_LEN				16
24
25/* motion vector size (bytes) for every macro block */
26#define HW_MB_STORE_SZ				64
27
28#define H264_MAX_FB_NUM				17
29#define HDR_PARSING_BUF_SZ			1024
30
31#define DEC_ERR_RET(ret)			((ret) >> 16)
32#define H264_ERR_NOT_VALID			3
33
34/**
35 * struct h264_fb - h264 decode frame buffer information
36 * @vdec_fb_va  : virtual address of struct vdec_fb
37 * @y_fb_dma    : dma address of Y frame buffer (luma)
38 * @c_fb_dma    : dma address of C frame buffer (chroma)
39 * @poc         : picture order count of frame buffer
40 * @reserved    : for 8 bytes alignment
41 */
42struct h264_fb {
43	uint64_t vdec_fb_va;
44	uint64_t y_fb_dma;
45	uint64_t c_fb_dma;
46	int32_t poc;
47	uint32_t reserved;
48};
49
50/**
51 * struct h264_ring_fb_list - ring frame buffer list
52 * @fb_list   : frame buffer array
53 * @read_idx  : read index
54 * @write_idx : write index
55 * @count     : buffer count in list
56 * @reserved  : for 8 bytes alignment
57 */
58struct h264_ring_fb_list {
59	struct h264_fb fb_list[H264_MAX_FB_NUM];
60	unsigned int read_idx;
61	unsigned int write_idx;
62	unsigned int count;
63	unsigned int reserved;
64};
65
66/**
67 * struct vdec_h264_dec_info - decode information
68 * @dpb_sz		: decoding picture buffer size
69 * @resolution_changed  : resolution change happen
70 * @realloc_mv_buf	: flag to notify driver to re-allocate mv buffer
71 * @reserved		: for 8 bytes alignment
72 * @bs_dma		: Input bit-stream buffer dma address
73 * @y_fb_dma		: Y frame buffer dma address
74 * @c_fb_dma		: C frame buffer dma address
75 * @vdec_fb_va		: VDEC frame buffer struct virtual address
76 */
77struct vdec_h264_dec_info {
78	uint32_t dpb_sz;
79	uint32_t resolution_changed;
80	uint32_t realloc_mv_buf;
81	uint32_t reserved;
82	uint64_t bs_dma;
83	uint64_t y_fb_dma;
84	uint64_t c_fb_dma;
85	uint64_t vdec_fb_va;
86};
87
88/**
89 * struct vdec_h264_vsi - shared memory for decode information exchange
90 *                        between VPU and Host.
91 *                        The memory is allocated by VPU then mapping to Host
92 *                        in vpu_dec_init() and freed in vpu_dec_deinit()
93 *                        by VPU.
94 *                        AP-W/R : AP is writer/reader on this item
95 *                        VPU-W/R: VPU is write/reader on this item
96 * @hdr_buf      : Header parsing buffer (AP-W, VPU-R)
97 * @pred_buf_dma : HW working predication buffer dma address (AP-W, VPU-R)
98 * @mv_buf_dma   : HW working motion vector buffer dma address (AP-W, VPU-R)
99 * @list_free    : free frame buffer ring list (AP-W/R, VPU-W)
100 * @list_disp    : display frame buffer ring list (AP-R, VPU-W)
101 * @dec          : decode information (AP-R, VPU-W)
102 * @pic          : picture information (AP-R, VPU-W)
103 * @crop         : crop information (AP-R, VPU-W)
104 */
105struct vdec_h264_vsi {
106	unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
107	uint64_t pred_buf_dma;
108	uint64_t mv_buf_dma[H264_MAX_FB_NUM];
109	struct h264_ring_fb_list list_free;
110	struct h264_ring_fb_list list_disp;
111	struct vdec_h264_dec_info dec;
112	struct vdec_pic_info pic;
113	struct v4l2_rect crop;
114};
115
116/**
117 * struct vdec_h264_inst - h264 decoder instance
118 * @num_nalu : how many nalus be decoded
119 * @ctx      : point to mtk_vcodec_dec_ctx
120 * @pred_buf : HW working predication buffer
121 * @mv_buf   : HW working motion vector buffer
122 * @vpu      : VPU instance
123 * @vsi      : VPU shared information
124 */
125struct vdec_h264_inst {
126	unsigned int num_nalu;
127	struct mtk_vcodec_dec_ctx *ctx;
128	struct mtk_vcodec_mem pred_buf;
129	struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
130	struct vdec_vpu_inst vpu;
131	struct vdec_h264_vsi *vsi;
132};
133
134static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
135{
136	return HW_MB_STORE_SZ * (width/MB_UNIT_LEN) * (height/MB_UNIT_LEN);
137}
138
139static int allocate_predication_buf(struct vdec_h264_inst *inst)
140{
141	int err = 0;
142
143	inst->pred_buf.size = BUF_PREDICTION_SZ;
144	err = mtk_vcodec_mem_alloc(inst->ctx, &inst->pred_buf);
145	if (err) {
146		mtk_vdec_err(inst->ctx, "failed to allocate ppl buf");
147		return err;
148	}
149
150	inst->vsi->pred_buf_dma = inst->pred_buf.dma_addr;
151	return 0;
152}
153
154static void free_predication_buf(struct vdec_h264_inst *inst)
155{
156	struct mtk_vcodec_mem *mem = NULL;
157
158	inst->vsi->pred_buf_dma = 0;
159	mem = &inst->pred_buf;
160	if (mem->va)
161		mtk_vcodec_mem_free(inst->ctx, mem);
162}
163
164static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
165{
166	int i;
167	int err;
168	struct mtk_vcodec_mem *mem = NULL;
169	unsigned int buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
170
171	for (i = 0; i < H264_MAX_FB_NUM; i++) {
172		mem = &inst->mv_buf[i];
173		if (mem->va)
174			mtk_vcodec_mem_free(inst->ctx, mem);
175		mem->size = buf_sz;
176		err = mtk_vcodec_mem_alloc(inst->ctx, mem);
177		if (err) {
178			mtk_vdec_err(inst->ctx, "failed to allocate mv buf");
179			return err;
180		}
181		inst->vsi->mv_buf_dma[i] = mem->dma_addr;
182	}
183
184	return 0;
185}
186
187static void free_mv_buf(struct vdec_h264_inst *inst)
188{
189	int i;
190	struct mtk_vcodec_mem *mem = NULL;
191
192	for (i = 0; i < H264_MAX_FB_NUM; i++) {
193		inst->vsi->mv_buf_dma[i] = 0;
194		mem = &inst->mv_buf[i];
195		if (mem->va)
196			mtk_vcodec_mem_free(inst->ctx, mem);
197	}
198}
199
200static int check_list_validity(struct vdec_h264_inst *inst, bool disp_list)
201{
202	struct h264_ring_fb_list *list;
203
204	list = disp_list ? &inst->vsi->list_disp : &inst->vsi->list_free;
205
206	if (list->count > H264_MAX_FB_NUM ||
207	    list->read_idx >= H264_MAX_FB_NUM ||
208	    list->write_idx >= H264_MAX_FB_NUM) {
209		mtk_vdec_err(inst->ctx, "%s list err: cnt=%d r_idx=%d w_idx=%d",
210			     disp_list ? "disp" : "free", list->count,
211			     list->read_idx, list->write_idx);
212		return -EINVAL;
213	}
214
215	return 0;
216}
217
218static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
219{
220	struct h264_ring_fb_list *list;
221
222	if (fb) {
223		if (check_list_validity(inst, false))
224			return;
225
226		list = &inst->vsi->list_free;
227		if (list->count == H264_MAX_FB_NUM) {
228			mtk_vdec_err(inst->ctx, "[FB] put fb free_list full");
229			return;
230		}
231
232		mtk_vdec_debug(inst->ctx, "[FB] put fb into free_list @(%p, %llx)",
233			       fb->base_y.va, (u64)fb->base_y.dma_addr);
234
235		list->fb_list[list->write_idx].vdec_fb_va = (u64)(uintptr_t)fb;
236		list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
237				  0 : list->write_idx + 1;
238		list->count++;
239	}
240}
241
242static void get_pic_info(struct vdec_h264_inst *inst,
243			 struct vdec_pic_info *pic)
244{
245	*pic = inst->vsi->pic;
246	mtk_vdec_debug(inst->ctx, "pic(%d, %d), buf(%d, %d)",
247		       pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
248	mtk_vdec_debug(inst->ctx, "fb size: Y(%d), C(%d)", pic->fb_sz[0], pic->fb_sz[1]);
249}
250
251static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_rect *cr)
252{
253	cr->left = inst->vsi->crop.left;
254	cr->top = inst->vsi->crop.top;
255	cr->width = inst->vsi->crop.width;
256	cr->height = inst->vsi->crop.height;
257
258	mtk_vdec_debug(inst->ctx, "l=%d, t=%d, w=%d, h=%d", cr->left, cr->top,
259		       cr->width, cr->height);
260}
261
262static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
263{
264	*dpb_sz = inst->vsi->dec.dpb_sz;
265	mtk_vdec_debug(inst->ctx, "sz=%d", *dpb_sz);
266}
267
268static int vdec_h264_init(struct mtk_vcodec_dec_ctx *ctx)
269{
270	struct vdec_h264_inst *inst = NULL;
271	int err;
272
273	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
274	if (!inst)
275		return -ENOMEM;
276
277	inst->ctx = ctx;
278
279	inst->vpu.id = IPI_VDEC_H264;
280	inst->vpu.ctx = ctx;
281
282	err = vpu_dec_init(&inst->vpu);
283	if (err) {
284		mtk_vdec_err(ctx, "vdec_h264 init err=%d", err);
285		goto error_free_inst;
286	}
287
288	inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
289	err = allocate_predication_buf(inst);
290	if (err)
291		goto error_deinit;
292
293	mtk_vdec_debug(ctx, "H264 Instance >> %p", inst);
294
295	ctx->drv_handle = inst;
296	return 0;
297
298error_deinit:
299	vpu_dec_deinit(&inst->vpu);
300
301error_free_inst:
302	kfree(inst);
303	return err;
304}
305
306static void vdec_h264_deinit(void *h_vdec)
307{
308	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
309
310	vpu_dec_deinit(&inst->vpu);
311	free_predication_buf(inst);
312	free_mv_buf(inst);
313
314	kfree(inst);
315}
316
317static int find_start_code(unsigned char *data, unsigned int data_sz)
318{
319	if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
320		return 3;
321
322	if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
323	    data[3] == 1)
324		return 4;
325
326	return -1;
327}
328
329static int vdec_h264_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
330			    struct vdec_fb *fb, bool *res_chg)
331{
332	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
333	struct vdec_vpu_inst *vpu = &inst->vpu;
334	int nal_start_idx = 0;
335	int err = 0;
336	unsigned int nal_start;
337	unsigned int nal_type;
338	unsigned char *buf;
339	unsigned int buf_sz;
340	unsigned int data[2];
341	uint64_t vdec_fb_va = (u64)(uintptr_t)fb;
342	uint64_t y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
343	uint64_t c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
344
345	mtk_vdec_debug(inst->ctx, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
346		       ++inst->num_nalu, y_fb_dma, c_fb_dma, fb);
347
348	/* bs NULL means flush decoder */
349	if (bs == NULL)
350		return vpu_dec_reset(vpu);
351
352	buf = (unsigned char *)bs->va;
353	buf_sz = bs->size;
354	nal_start_idx = find_start_code(buf, buf_sz);
355	if (nal_start_idx < 0) {
356		mtk_vdec_err(inst->ctx, "invalid nal start code");
357		err = -EIO;
358		goto err_free_fb_out;
359	}
360
361	nal_start = buf[nal_start_idx];
362	nal_type = NAL_TYPE(buf[nal_start_idx]);
363	mtk_vdec_debug(inst->ctx, "\n + NALU[%d] type %d +\n", inst->num_nalu,
364		       nal_type);
365
366	if (nal_type == NAL_H264_PPS) {
367		buf_sz -= nal_start_idx;
368		if (buf_sz > HDR_PARSING_BUF_SZ) {
369			err = -EILSEQ;
370			goto err_free_fb_out;
371		}
372		memcpy(inst->vsi->hdr_buf, buf + nal_start_idx, buf_sz);
373	}
374
375	inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
376	inst->vsi->dec.y_fb_dma = y_fb_dma;
377	inst->vsi->dec.c_fb_dma = c_fb_dma;
378	inst->vsi->dec.vdec_fb_va = vdec_fb_va;
379
380	data[0] = buf_sz;
381	data[1] = nal_start;
382	err = vpu_dec_start(vpu, data, 2);
383	if (err) {
384		if (err > 0 && (DEC_ERR_RET(err) == H264_ERR_NOT_VALID)) {
385			mtk_vdec_err(inst->ctx, "- error bitstream - err = %d -", err);
386			err = -EIO;
387		}
388		goto err_free_fb_out;
389	}
390
391	*res_chg = inst->vsi->dec.resolution_changed;
392	if (*res_chg) {
393		struct vdec_pic_info pic;
394
395		mtk_vdec_debug(inst->ctx, "- resolution changed -");
396		get_pic_info(inst, &pic);
397
398		if (inst->vsi->dec.realloc_mv_buf) {
399			err = alloc_mv_buf(inst, &pic);
400			if (err)
401				goto err_free_fb_out;
402		}
403	}
404
405	if (nal_type == NAL_NON_IDR_SLICE || nal_type == NAL_IDR_SLICE) {
406		/* wait decoder done interrupt */
407		err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
408						   MTK_INST_IRQ_RECEIVED,
409						   WAIT_INTR_TIMEOUT_MS, 0);
410		if (err)
411			goto err_free_fb_out;
412
413		vpu_dec_end(vpu);
414	}
415
416	mtk_vdec_debug(inst->ctx, "\n - NALU[%d] type=%d -\n", inst->num_nalu, nal_type);
417	return 0;
418
419err_free_fb_out:
420	put_fb_to_free(inst, fb);
421	mtk_vdec_err(inst->ctx, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
422	return err;
423}
424
425static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
426			     struct h264_ring_fb_list *list,
427			     bool disp_list, struct vdec_fb **out_fb)
428{
429	struct vdec_fb *fb;
430
431	if (check_list_validity(inst, disp_list))
432		return;
433
434	if (list->count == 0) {
435		mtk_vdec_debug(inst->ctx, "[FB] there is no %s fb", disp_list ? "disp" : "free");
436		*out_fb = NULL;
437		return;
438	}
439
440	fb = (struct vdec_fb *)
441		(uintptr_t)list->fb_list[list->read_idx].vdec_fb_va;
442	fb->status |= (disp_list ? FB_ST_DISPLAY : FB_ST_FREE);
443
444	*out_fb = fb;
445	mtk_vdec_debug(inst->ctx, "[FB] get %s fb st=%d poc=%d %llx",
446		       disp_list ? "disp" : "free",
447		       fb->status, list->fb_list[list->read_idx].poc,
448		       list->fb_list[list->read_idx].vdec_fb_va);
449
450	list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
451			 0 : list->read_idx + 1;
452	list->count--;
453}
454
455static int vdec_h264_get_param(void *h_vdec, enum vdec_get_param_type type,
456			       void *out)
457{
458	struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
459
460	switch (type) {
461	case GET_PARAM_DISP_FRAME_BUFFER:
462		vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
463		break;
464
465	case GET_PARAM_FREE_FRAME_BUFFER:
466		vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
467		break;
468
469	case GET_PARAM_PIC_INFO:
470		get_pic_info(inst, out);
471		break;
472
473	case GET_PARAM_DPB_SIZE:
474		get_dpb_size(inst, out);
475		break;
476
477	case GET_PARAM_CROP_INFO:
478		get_crop_info(inst, out);
479		break;
480
481	default:
482		mtk_vdec_err(inst->ctx, "invalid get parameter type=%d", type);
483		return -EINVAL;
484	}
485
486	return 0;
487}
488
489const struct vdec_common_if vdec_h264_if = {
490	.init		= vdec_h264_init,
491	.decode		= vdec_h264_decode,
492	.get_param	= vdec_h264_get_param,
493	.deinit		= vdec_h264_deinit,
494};
495